map_task.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import "fmt"
  3. func main() {
  4. // foo1()
  5. // foo2()
  6. cities()
  7. }
  8. func work(x int) int {
  9. return x
  10. }
  11. func foo1() {
  12. myMap := make(map[int]int)
  13. input := [10]int{}
  14. for i := 0; i < 10; i++ {
  15. fmt.Scan(&input[i])
  16. if value, ok := myMap[input[i]]; ok {
  17. // fmt.Println("Value from cache: ", value)
  18. fmt.Printf("%d ", value)
  19. } else {
  20. myMap[input[i]] = work(input[i])
  21. // fmt.Println(myMap[input[i]])
  22. fmt.Printf("%d ", myMap[input[i]])
  23. }
  24. }
  25. }
  26. func foo2() {
  27. cache := make(map[int]int, 10)
  28. for n, i := 0, 0; i < 10; i++ {
  29. fmt.Scan(&n)
  30. if _, exists := cache[n]; !exists {
  31. cache[n] = work(n)
  32. }
  33. fmt.Print(cache[n], " ")
  34. }
  35. }
  36. func cities() {
  37. groupCity := map[int][]string{
  38. 10: []string{"Ивановка", "Грязь"},
  39. 100: []string{"Королев", "Электроугли"},
  40. 1000: []string{"Москва"},
  41. }
  42. cityPopulation := map[string]int{
  43. "Королев": 30,
  44. "Москва": 200,
  45. }
  46. // fmt.Println(groupCity)
  47. // fmt.Println(cityPopulation)
  48. for pKey, _ := range cityPopulation {
  49. //fmt.Println("Searching key", pKey)
  50. for cKey, _ := range groupCity {
  51. //fmt.Println(cKey, cValue)
  52. if cKey != 100 {
  53. for _, sValue := range groupCity[cKey] {
  54. //fmt.Println(sValue)
  55. if sValue == pKey {
  56. delete(cityPopulation, pKey)
  57. }
  58. }
  59. }
  60. }
  61. }
  62. fmt.Println("...")
  63. fmt.Println(cityPopulation)
  64. }