kAldown

clock

Nov 11th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. def angle(time):
  2.     hour, minute = time.split(':')
  3.     hour = int(hour) % 12
  4.     minute = int(minute)
  5.     hour_rad = 30  # 90 / 3 (3 hours in every quarter)
  6.     minute_rad = 6  # 90 / 15
  7.     def hour_shift(minute):
  8.         return minute / 60. * hour_rad
  9.     minute_uni = minute_rad * minute
  10.     hour_uni = hour_rad * hour + hour_shift(minute)
  11.     result = max(minute_uni, hour_uni) - min(minute_uni, hour_uni)
  12.     return result if result <= 180 else abs(180 - result)
  13.  
  14.  
  15. # test
  16. def test():
  17.     with open('clock_test.txt', 'r') as afile:
  18.         for line in afile:
  19.             input, expected = line.split()
  20.             result = angle(input)
  21.             if not float(expected) == result:
  22.                 raise Exception('angle(%s) is not %s, but %s' %
  23.                             (input, expected, result)
  24.                         )
  25. test()
Advertisement
Add Comment
Please, Sign In to add comment