-
Notifications
You must be signed in to change notification settings - Fork 0
/
303.py
54 lines (39 loc) · 1.15 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Problem:
Given a clock time in hh:mm format, determine, to the nearest degree, the angle between
the hour and the minute hands.
Bonus: When, during the course of a day, will the angle be zero?
"""
HOUR_ANGLE = {
1: (1 / 12) * 360,
2: (2 / 12) * 360,
3: (3 / 12) * 360,
4: (4 / 12) * 360,
5: (5 / 12) * 360,
6: (6 / 12) * 360,
7: (7 / 12) * 360,
8: (8 / 12) * 360,
9: (9 / 12) * 360,
10: (10 / 12) * 360,
11: (11 / 12) * 360,
12: (12 / 12) * 360,
}
def get_displaced_hour_angle(mm: int) -> float:
return (mm / 60) * (360 / 12)
def get_minutes_angle(mm: int) -> float:
return (mm / 60) * 360
def get_angle_between_arms(time: str) -> int:
hh, mm = [int(elem) for elem in time.split(":")]
hour_angle = (HOUR_ANGLE[hh] + get_displaced_hour_angle(mm)) % 360
minute_angle = get_minutes_angle(mm)
return round(abs(hour_angle - minute_angle))
if __name__ == "__main__":
print(get_angle_between_arms("12:20"))
print(get_angle_between_arms("12:00"))
print(get_angle_between_arms("6:30"))
print(get_angle_between_arms("3:45"))
"""
SPECS:
TIME COMPLEXITY: O(1)
SPACE COMPLEXITY: O(1)
"""