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