Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Broken Clock by aya.kanazawa
import datetime
import re
def broken_clock(starting_time, wrong_time, error_description):
m = re.match('([\+\-]\d+) (second|minute|hour)s* at (\d+) (second|minute|hour)s*', error_description)
mg = m.groups() # ex.['+5', 'second', '10', 'second']
dic = {'second': 1, 'minute': 60, 'hour': 60*60}
progress_by_sec = (int(mg[0]) * dic[mg[1]] + int(mg[2]) * dic[mg[3]]) \
/ (int(mg[2]) * dic[mg[3]])
w_time = datetime.datetime.strptime(wrong_time, '%H:%M:%S')
s_time = datetime.datetime.strptime(starting_time, '%H:%M:%S')
diff = (w_time - s_time).seconds
return(s_time + datetime.timedelta(seconds= diff / progress_by_sec)).strftime("%H:%M:%S")
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == "__main__":
assert broken_clock('06:10:00', '06:10:15', '-5 seconds at 10 seconds') == '06:10:30', 'Second example'
assert broken_clock('13:00:00', '14:01:00', '+1 second at 1 minute') == '14:00:00', 'Third example'
assert broken_clock('01:05:05', '04:05:05', '-1 hour at 2 hours') == '07:05:05', 'Fourth example'
assert broken_clock('00:00:00', '00:00:15', '+5 seconds at 10 seconds') == '00:00:10', "First example"
assert broken_clock('00:00:00', '00:00:30', '+2 seconds at 6 seconds') == '00:00:22', 'Fifth example'
April 29, 2019
Comments: