资讯专栏INFORMATION COLUMN

【python】Human readable duration format 时间格式转换

liangzai_cool / 2767人阅读

摘要:背景上的一道原题,通过格式划字符串精简了代码结构,省去了很多条件判断语句。题目描述解题思路题目是很简单的,关键是如何优雅地完成是否在当前时间单位后添加和或者,我的代码里运用了很多语句。代码感想表达式真是一个神器。

背景

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 表达式真是一个神器。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/38128.html

相关文章

  • hrn(Human Readable Number)——数字格式

    摘要:是一个非常简单的库,用来格式化数字,变成可读的格式,可以自定义格式。万高级自定义通过自定义,可以将文件数时间间隔等数字格式化成自己想要的格式。可以随意自己定义语言和样式。 hrn is short for Human Readable Number, a simple javascript for browserjs / nodejs library to format number ...

    mylxsw 评论0 收藏0
  • python深度神经网络tensorflow练习好一点的实体模型开展图像分类

      此篇文章主要是给大家介绍了python深度神经网络tensorflow练习好一点的实体模型开展图像分类实例详细说明,感兴趣的小伙伴可以参考借鉴一下,希望可以有一定的帮助,祝愿大家多多的发展,尽早涨薪。  文章正文  Google在各类图象数据库系统ImageNet上练习好啦1个Inception-v3实体模型,这一实体模型大家也可以用来进去图像分类。  下载地址:https://pan.bai...

    89542767 评论0 收藏0
  • PyTips 0x11 - Python 时间与日期

    摘要:项目地址时间和日期可能涉及到不同的时区格式,同时又经常需要作为时间戳保存,有时候还需要进行一些加减操作,因此处理起来通常会因为方法太多而无从下手。中与时间和日期相关的标准库有个和。 项目地址:https://git.io/pytips 时间和日期可能涉及到不同的时区、格式,同时又经常需要作为时间戳保存,有时候还需要进行一些加减操作,因此处理起来通常会因为方法太多而无从下手。Python...

    2501207950 评论0 收藏0
  • Yii2 GridView的使用方法

    摘要:是实现网格视图的小部件,一般用于报表视图的展示。就是连续的列,主要用于网格的行号,属于自增式的列。指定处理的类,必须。 Yii2 GridView是实现yii网格视图的小部件,一般用于报表视图的展示。今天,结合DataProvider(ArrayDataProvider以及SqlDataProvider)说一下GridView中的几个Columns(SerialColumn,DataC...

    Paul_King 评论0 收藏0
  • Python 2.7终结于7个月后,这是你需要了解的3.X炫酷新特性

    摘要:截止到月号上午点,将终结于在这一段时间中,很多优秀开源项目与库已经停止了对的支持。除了,还提供了一种通过进行字符串插入的灵活方法。扩展的可迭代对象解包最低版本为对于这个特性,代码就说明了一切。从 3.0 到 3.8,Python 3 已经更新了一波又一波,但似乎我们用起来和 2.7 没有太大区别?以前该怎么写 2.7 的代码现在就怎么写,只不过少数表达方式变了而已。在这篇文章中,作者介绍了 ...

    chadLi 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<