-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock_angle.py
More file actions
91 lines (67 loc) · 2.01 KB
/
clock_angle.py
File metadata and controls
91 lines (67 loc) · 2.01 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
import collections
import sys
HOURS = 12
MINUTES = 60
# Calculate the degrees each hand moves by each minute.
HOUR_HAND = float(360) / HOURS / MINUTES
MINUTE_HAND = float(360) / MINUTES
HourMinute = collections.namedtuple('HourMinute', 'hour minute')
class Error(Exception):
"""Custom Exception for module."""
def calculate_angle(hour, minute):
"""Calculate the angle between hour hand and minute hand.
Args:
hour: int
minute: int
Return:
int, the minimum angle between the hour and minute hand.
"""
if hour not in range(HOURS + 1) or minute not in range(MINUTES + 1):
raise Error("Wrong input", hour, minute)
hour = 0 if hour == HOURS else hour
minute = 0 if minute == MINUTES else minute
hour_angle = int(HOUR_HAND * (hour * MINUTES + minute))
minute_angle = int(MINUTE_HAND * minute)
angle = abs(hour_angle - minute_angle)
return min(abs(360 - angle), angle)
def split_hour_minute(arg):
"""Split a string into hour minute.
Args:
arg: str, HH:MM string.
Returns:
HourMinute named tuple or False
"""
time_value = [int(n) for n in arg.split(':') if n.isdigit()]
if len(time_value) == 2:
return HourMinute(hour=time_value[0], minute=time_value[1])
else:
return False
def tests():
"""Some simple tests."""
assert HOUR_HAND == 0.5
assert MINUTE_HAND == 6
assert calculate_angle(9, 60) == 90
assert calculate_angle(3, 30) == 75
try:
calculate_angle(24, 0)
except Error:
assert True
else:
assert False
assert split_hour_minute('3:30').hour == 3
assert split_hour_minute('3:30').minute == 30
def main(arg):
"""Print angle between the hour and minute hand in degrees.
Args:
arg: str, HH:MM from stdin.
"""
time_value = split_hour_minute(arg)
angle = calculate_angle(time_value.hour, time_value.minute)
print (
'The angle between the hour and minute '
'hand for {0} is {1} degrees.'.format(arg, angle))
if __name__ == '__main__':
tests()
if sys.argv[1:]:
main(' '.join(sys.argv[1:]))