forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_303.py
30 lines (20 loc) · 822 Bytes
/
problem_303.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from math import isclose
FLOAT_EQUALITY_TOLERANCE = 0.5
def get_angle_for_hour(hour: int, minute: int):
minute_offset = minute / 12
hour_angle = (hour * 30) + minute_offset
return hour_angle
def get_angle_for_minute(minute: int):
return minute * 6
def get_angle(hhmm_time: str):
hour, minute = map(int, hhmm_time.split(":"))
hour %= 12
ha = get_angle_for_hour(hour, minute)
ma = get_angle_for_minute(minute)
angle = abs(ha - ma)
return angle if angle < 180 else 360 - angle
# Tests
assert isclose(get_angle("12:20"), 118, abs_tol=FLOAT_EQUALITY_TOLERANCE)
assert isclose(get_angle("12:00"), 0, abs_tol=FLOAT_EQUALITY_TOLERANCE)
assert isclose(get_angle("6:30"), 3, abs_tol=FLOAT_EQUALITY_TOLERANCE)
assert isclose(get_angle("3:45"), 176, abs_tol=FLOAT_EQUALITY_TOLERANCE)