Skip to content

Instantly share code, notes, and snippets.

@ddaws
Forked from BeOleg/hour_overlap.py
Last active October 22, 2016 00:57
Show Gist options
  • Save ddaws/40925a216a656231acad4613163a00d6 to your computer and use it in GitHub Desktop.
Save ddaws/40925a216a656231acad4613163a00d6 to your computer and use it in GitHub Desktop.
Oleg's Challenge
# 1. Create a Python function that gets an integer between 1 - 12 (as an hour) and returns the exact time in hours
# and minutes at which the hands of the clock overlap at this hour
# (for example, for 1, the result will be close to 1:06)
# 2. create a unit test for this function, using 'assert', or the Python 'unittest' framework
# 3. [BONUS]
# a. add also seconds calculation to the return value of the function.
# b. calculate the angle of the hands in 360 degrees system
# taking 12:00 (noon, midnight) as a point of reference
# ATTENTION: You do not need to import any modules to implement this task
DEGREES_PER_MINUTE_RATIO = 360 / 60
HOUR_RATIO_INCREMENT = 1 / 12
MINUTES_PER_HOUR = 60
SECONDS_PER_MINUTE = 60
ACCURACY = 0.001 # set arbitrarily based on a changes in output
def calculate_clock_position(hour_position, minute_position=0):
'''
Calculate how far the hour hand moves forward as the minute hand "catches up" to it.
hour_position - The ratio from 0 to 1 that represents the position around the clock.
minute_position - The ratio from 0 to 1 that represents the position around the clock.
'''
# minute hand moves forward towards hour hand
next_minute_position = hour_position
# the hour hand moves forward in this period
hour_position += (hour_position - minute_position) * HOUR_RATIO_INCREMENT
# if we exceed our accuracy this will suffice
if hour_position - next_minute_position < ACCURACY:
return (hour_position, next_minute_position)
else:
return calculate_clock_position(hour_position, next_minute_position)
def hour_overlap(hour):
# confirm our value is an integer in the range 1 to 12 inclusively
assert isinstance(hour, int)
assert 1 <= hour and hour <= 12
# the simple case of 12
if hour == 12:
return {
"hours": hour,
"mins": 0,
"secs": 0,
"min_angle": 0
}
hour_position, minute_position = calculate_clock_position(hour / 12)
# calculate minutes, seconds, and angle
mins = hour_position * MINUTES_PER_HOUR
secs = mins % 1 * SECONDS_PER_MINUTE
angle = hour_position * 360
return {
"hours": hour,
"mins": int(mins),
"secs": int(secs),
"min_angle": angle # float
}
def test_dict_instance():
try:
res = hour_overlap(1)
assert isinstance(res, dict)
except AssertionError as e:
print("Oh, snap! the test has failed. exception: {}").format(e)
def test_value_one():
try:
res = hour_overlap(1)
assert res['hours'] == 1
assert res['mins'] == 5
assert res['secs'] == 27
assert int(res['min_angle']) == 32
except AssertionError as e:
print('Invalid response calculating hour overlap at hour 1.')
print('{}'.format(e))
def test_value_twelve():
try:
res = hour_overlap(12)
assert res['hours'] == 12
assert res['mins'] == 0
assert res['secs'] == 0
assert res['min_angle'] == 0
except AssertionError as e:
print('Invalid response calculating hour overlap at hour 12.')
print('{}'.format(e))
if __name__ == '__main__':
for i in range(1, 12):
print('{hours:0>2}:{mins:0>2}:{secs:0>2} @ {min_angle:06.2f} degrees'.format(**hour_overlap(i)))
test_dict_instance()
test_value_one()
test_value_twelve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment