fork download
  1. from math import acos, sin, cos, radians, floor
  2. RADIUS_MILES = 3963
  3.  
  4. class Solution:
  5. def __init__(self):
  6. self.city_map = {}
  7. pass
  8.  
  9. def handle_location(self, line_arr):
  10. city = line_arr[1]
  11. lat_rad = radians(int(line_arr[2]))
  12. long_rad = radians(int(line_arr[3]))
  13. self.city_map[city] = [lat_rad,long_rad]
  14.  
  15. def calculate_distance(self,line_arr):
  16. city_1 = self.city_map[line_arr[1]]
  17. city_2 = self.city_map[line_arr[2]]
  18. abs_long_dif = abs(city_1[2]-city_2[2])
  19. sin_pro = sin(city_1[0])*sin(city_2[0])
  20. cos_pro = cos(city_1[0])*cos(city_2[0])*abs_long_dif
  21. distance = floor(RADIUS_MILES*acos(sin_pro+cos_pro))
  22. return ":".join(line_arr[1:]+[distance])
  23.  
  24.  
  25. def process(self,line):
  26. line_arr = line.split(":")
  27. if(line_arr[0] == "LOC"):
  28. self.handle_location(line_arr)
  29. return line_arr[1]
  30. else:
  31. return self.calculate_distance(line_arr)
  32. pass
  33.  
  34. sol = Solution()
  35. print(sol.process("LOC:CHI:41.836944:-87.684722"))
  36. print(sol.process("LOC:NYC:40.7127:-74.0059"))
  37. print(sol.process("TRIP:C0FFEE1C:CHI:NYC"))
  38.  
Runtime error #stdin #stdout #stderr 0.12s 23500KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 35, in <module>
  File "./prog.py", line 28, in process
  File "./prog.py", line 11, in handle_location
ValueError: invalid literal for int() with base 10: '41.836944'