Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Broken Clock by kurosawa4434
from datetime import datetime
from re import match
HOUR = 'hour'
MINUTE = 'minute'
SECOND = 'second'
def broken_clock(starting_time, wrong_time, error_description):
s = match('(\d\d):(\d\d):(\d\d)', starting_time)
w = match('(\d\d):(\d\d):(\d\d)', wrong_time)
start_time = datetime(2016, 1, 1, hour=int(s.group(1)), minute=int(s.group(2)), second=int(s.group(3)))
wrong_time = datetime(2016, 1, 1, hour=int(w.group(1)), minute=int(w.group(2)), second=int(w.group(3)))
ed = match('(.+) (.+) at (.+) (.+)', error_description)
diff_time = int(ed.group(1))
diff_unit = ed.group(2)
elapsed_time = int(ed.group(3))
elapsed_unit = ed.group(4)
def to_seconds(t, u):
if u[:6] == SECOND:
return t
if u[:6] == MINUTE:
return t * 60
if u[:4] == HOUR:
return t * 3600
diff_sec = to_seconds(diff_time, diff_unit)
elapsed_sec = to_seconds(elapsed_time, elapsed_unit)
answer_time = start_time + (wrong_time - start_time) * (elapsed_sec / (diff_sec + elapsed_sec))
return answer_time.strftime("%H:%M:%S")
July 24, 2016
Comments: