【python】Human readable duration format 时间格式转换
admin
2023-07-31 01:47:21
0

背景

CodeWar上的一道原题,通过格式划字符串精简了代码结构,省去了很多条件判断语句。

题目描述

Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.

The function must accept a non-negative integer. If it is zero, it just returns \”now\”. Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.

It is much easier to understand with an example:

format_duration(62) # returns \”1 minute and 2 seconds\”
format_duration(3662) # returns \”1 hour, 1 minute and 2 seconds\”

Note that spaces are important.

Detailed rules

The resulting expression is made of components like 4 seconds, 1 year, etc. In general, a positive integer and one of the valid units of time, separated by a space. The unit of time is used in plural if the integer is greater than 1.

The components are separated by a comma and a space (\”, \”). Except the last component, which is separated by \” and \”, just like it would be written in English.

A more significant units of time will occur before than a least significant one. Therefore, 1 second and 1 year is not correct, but 1 year and 1 second is.

Different components have different unit of times. So there is not repeated units like in 5 seconds and 1 second.

A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0 seconds is not valid, but it should be just 1 minute.

A unit of time must be used \”as much as possible\”. It means that the function should not return 61 seconds, but 1 minute and 1 second instead. Formally, the duration specified by of a component must not be greater than any valid more significant unit of time.

For the purpose of this Kata, a year is 365 days and a day is 24 hours.

解题思路

题目是很简单的,关键是如何优雅地完成是否在当前时间单位后添加\’s\’和\’,\’或者\’and\’,我的代码里运用了很多

true_value if condition else false_value

语句。

代码

def format_duration(seconds):
    if seconds == 0: return \"now\"
    origin = seconds
    dic = {
        \'year\': 60 * 60 * 24 * 365,
        \'day\': 60 * 60 * 24,
        \'hour\': 60 * 60,
        \'minute\': 60,
        \'second\': 1
    }
    spent = {}
    ans = \"\"
    for x in [\'year\',\'day\',\'hour\',\'minute\',\'second\']:
        spent[x] = seconds // dic[x]
        ans += \"{}{} {}{}\".format(\'\' if seconds == origin else \' and \' if seconds % dic[x] == 0 else \', \',spent[x],x,\'s\' if spent[x] > 1 else \'\') if spent[x] > 0 else \'\'
        seconds %= dic[x]
    return  ans

感想

if else 表达式真是一个神器。

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...