반응형
Problem: py.checkio.org/en/mission/date-and-time-converter/
My Solution:
import datetime
def date_time(time: str) -> str:
#replace this for solution
DATE, TIME = time.split(' ')
day, mon, year = list(map(lambda x: int(x), DATE.split('.')))
hour, min = list(map(lambda x: int(x), TIME.split(':')))
d = datetime.date(year, mon, day)
mon = d.strftime("%B")
if hour == 1:
if min == 1:
return "%d %s %d year %d hour %d minute" % (day, mon, year, hour, min)
else:
return "%d %s %d year %d hour %d minutes" % (day, mon, year, hour, min)
else:
return "%d %s %d year %d hours %d minutes" % (day, mon, year, hour, min)
if __name__ == '__main__':
print("Example:")
print(date_time('01.01.2000 00:00'))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert date_time("01.01.2000 00:00") == "1 January 2000 year 0 hours 0 minutes", "Millenium"
assert date_time("09.05.1945 06:30") == "9 May 1945 year 6 hours 30 minutes", "Victory"
assert date_time("20.11.1990 03:55") == "20 November 1990 year 3 hours 55 minutes", "Somebody was born"
print("Coding complete? Click 'Check' to earn cool rewards!")
A good solution that I see
from datetime import datetime
def checkio(time):
dt = datetime.strptime(time, '%d.%m.%Y %H:%M')
hour = 'hour' if dt.hour == 1 else 'hours'
minute = 'minute' if dt.minute == 1 else 'minutes'
return dt.strftime(f'%-d %B %Y year %-H {hour} %-M {minute}')
댓글