alex0sunny

nearest_stop

Sep 14th, 2021 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. MAX_DIST = 20
  2.  
  3.  
  4. def calc_dist(xy1, xy2):
  5.     return ((xy1[0] - xy2[0]) ** 2 + (xy1[1] - xy2[1]) ** 2) ** (1 / 2)
  6.  
  7.  
  8. def nearest_stop(metro_data, bus_data):
  9.     square_set = set()
  10.     for x in range(-20, 21):
  11.         for y in range(-20, 21):
  12.             if calc_dist((x, y), (0, 0)) <= MAX_DIST:
  13.                 square_set.add((x, y))
  14.     max_count = i_max = -1
  15.     for i, (metro_x, metro_y) in enumerate(metro_data):
  16.         count = 0
  17.         for bus_x, bus_y in bus_data:
  18.             if (bus_x - metro_x, bus_y - metro_y) in square_set:
  19.                 count += 1
  20.         if count > max_count:
  21.             max_count = count
  22.             i_max = i
  23.     return i_max + 1
  24.  
  25.  
  26. a = [[-1, 0], [1, 0], [2, 5]]
  27. b = [[10, 0], [20, 0], [22, 5]]
  28. assert(nearest_stop(a, b) == 3)
  29.  
  30. a = [[-1, 0], [1, 0], [0, 5]]
  31. b = [[10, 0], [20, 0], [20, 5]]
  32. assert(nearest_stop(a, b) == 2)
  33.  
  34. a_str = '-87 -73 -68 -71 31 -27 50 -8 44 -7 -33 -106 -74 -50 -30 -37 -42 -92 -78 -44'
  35. b_str = '-106 -55 -37 -46 -91 -40 3 -56 -22 -58 -85 -49 -111 -74 43 -17 60 -3 -81 -73 -40 -40 21 -30 -91 -35 -31 -62 -51 -103'
  36. a = list(map(int, a_str.split()))
  37. a = [[x, y] for x, y in zip(a[::2], a[1::2])]
  38. b = list(map(int, b_str.split()))
  39. b = [[x, y] for x, y in zip(b[::2], b[1::2])]
  40. assert(nearest_stop(a, b) == 10)
  41.  
Add Comment
Please, Sign In to add comment