CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思路和实现代码,同时也学习学习其他大神写的代码。
CheckiO 官网:https://checkio.org/
我的 CheckiO 主页:https://py.checkio.org/user/TRHX/
CheckiO 题解系列专栏:https://itrhx.blog.csdn.net/category_9536424.html
CheckiO 所有题解源代码:https://github.com/TRHX/Python-CheckiO-Exercise
题目描述
【Date and Time Converter】:计算机日期和时间格式仅由数字组成,例如:21.05.2018 16:30
,人们更喜欢看到这样的内容:21 May 2018 year 16 hours 30 minutes
,您的任务就是将计算机格式的日期转换成人们更喜欢的日期格式。
【链接】:https://py.checkio.org/mission/date-and-time-converter/
【输入】:计算机格式的日期和时间(字符串)
【输出】:人们更喜欢的日期格式(字符串)
【前提】:0 < date <= 31;0 < month <= 12;0 < year <= 3000;0 < hours < 24;0 < minutes < 60
【范例】:
date_time("01.01.2000 00:00") == "1 January 2000 year 0 hours 0 minutes"
date_time("19.09.2999 01:59") == "19 September 2999 year 1 hour 59 minutes"
date_time("21.10.1999 18:01") == "21 October 1999 year 18 hours 1 minute"
NB: words "hour" and "minute" are used only when time is 01:mm (1 hour) or hh:01 (1 minute).
In other cases it should be used "hours" and "minutes".
解题思路
这里需要注意的是,如果小时和分钟是 1 的话,就要使用 hour
和 minute
,其他情况下使用 hours
和 minutes
;
另外,对于日期、小时和分钟,如果原始字符串是 01,那么转变过后是 1(去掉前面的 0);
将原来的字符串进行处理,去掉小数点和冒号,以空格为分割符分割字符串,转换成列表形式;
将月份的英文用一个列表表示,用 int()
方法去掉多余的数字 0,挨个替换原字符串,最后再拼接成新的字符串即可。
代码实现
def date_time(time: str) -> str:month = ['January', 'February', 'March', 'April', 'May','June', 'July', 'August', 'September', 'October','November', 'December']time = time.replace('.', ' ').replace(':', ' ').split()time[0] = str(int(time[0]))time[1] = month[int(time[1])-1]time[2] = time[2] + ' year'if time[3] == '01':time[3] = '1 hour'else:time[3] = str(int(time[3])) + ' hours'if time[4] == '01':time[4] = '1 minute'else:time[4] = str(int(time[4])) + ' minutes'time = ' '.join(time)return timeif __name__ == '__main__':print("Example:")print(date_time('01.01.2000 00:00'))#These "asserts" using only for self-checking and not necessary for auto-testingassert 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!")
大神解答
大神解答 NO.1
from datetime import datetimedef 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}')
大神解答 NO.2
from datetime import datetimedef date_time(time):t = datetime.strptime(time, '%d.%m.%Y %H:%M')y, m, d, h, mi = t.year, datetime.strftime(t, '%B'), t.day, t.hour, t.minutesuffix = lambda n: 's' if n != 1 else ''return f'{d} {m} {y} year {h} hour{suffix(h)} {mi} minute{suffix(mi)}'
大神解答 NO.3
from datetime import datetimedef checkio(dt):dt = datetime.strptime(dt, '%d.%m.%Y %H:%M')p = lambda attr: attr + 's' * (getattr(dt, attr) != 1)return dt.strftime(f'%-d %B %Y year %-H {p("hour")} %-M {p("minute")}')