'''
Created on 2015. 12. 3.

@author: User
'''
6.4 Doomsday clock
def clock(minutes):
"""
>>> clock(0)
กฏ00:00กฏ
>>> clock(7)
กฏ23:53กฏ
>>> clock(123)
กฏ21:57กฏ
"""
# convert minutes before midnight into minutes since midnight
minutes = (24 * 60 - int(minutes)) % (24 * 60)
# convert minutes since midnight into hours and minutes on 24-hour clock
hours, minutes = minutes // 60, minutes % 60
# representation on 24-hour clock in format hh:mm
return กฏ{:>02d}:{:>02d}กฏ.format(hours, minutes)
def progress(adjustments):
"""
>>> adjustments = กฏ1947 7,1949 3,1953 2,1960 7,1963 12,1968 7,1969 10,1972 12,1974 9,1980
7,1981 4,1983 3,1984 3กฏ
>>> progress(adjustments)
((1947, กฏ23:53กฏ), (1949, กฏ23:57กฏ), (1953, กฏ23:58กฏ), (1960, กฏ23:53กฏ), (1963, กฏ23:48กฏ),
(1968, กฏ23:53กฏ), (1969, กฏ23:50กฏ), (1972, กฏ23:48กฏ), (1974, กฏ23:51กฏ), (1980, กฏ23:53กฏ),
(1981, กฏ23:56กฏ), (1983, กฏ23:57กฏ), (1984, กฏ23:57กฏ))
"""
# intialize list
progress = []
# split string into list of adjustments and process individual adjustments
for adjustment in adjustments.split(กฏ,กฏ):
# split adjustment into year and number of minutes before midnight
year, minutes = adjustment.split()
# append year and representation on 24-hour clock to list
progress.append((int(year), clock(int(minutes))))
# convert list into tuple
return tuple(progress)
def threat(year, clock):
"""
>>> adjustments = กฏ1947 7,1949 3,1953 2,1960 7,1963 12,1968 7,1969 10,1972 12,1974 9,1980
7,1981 4,1983 3,1984 3กฏ
>>> doomsdayclock = progress(adjustments)
>>> threat(1974, doomsdayclock)
กฏ23:51กฏ
22
>>> threat(1950, doomsdayclock)
กฏ23:57กฏ
>>> threat(1965, doomsdayclock)
กฏ23:48กฏ
"""
# find latest adjustment preceding or equal to the given year
index = -1
while clock[index][0] > year:
index -= 1
# return time set at latest adjustment
return clock[index][1]
if __name__ == กฏ__main__กฏ:
import doctest
doctest.testmod(