成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

【python】Human readable duration format 時(shí)間格式轉(zhuǎn)換

liangzai_cool / 2930人閱讀

摘要:背景上的一道原題,通過格式劃字符串精簡了代碼結(jié)構(gòu),省去了很多條件判斷語句。題目描述解題思路題目是很簡單的,關(guān)鍵是如何優(yōu)雅地完成是否在當(dāng)前時(shí)間單位后添加和或者,我的代碼里運(yùn)用了很多語句。代碼感想表達(dá)式真是一個(gè)神器。

背景

CodeWar上的一道原題,通過格式劃字符串精簡了代碼結(jié)構(gòu),省去了很多條件判斷語句。

題目描述

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.

解題思路

題目是很簡單的,關(guān)鍵是如何優(yōu)雅地完成是否在當(dāng)前時(shí)間單位后添加"s"和","或者"and",我的代碼里運(yùn)用了很多

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 表達(dá)式真是一個(gè)神器。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/38128.html

相關(guān)文章

  • hrn(Human Readable Number)——數(shù)字格式

    摘要:是一個(gè)非常簡單的庫,用來格式化數(shù)字,變成可讀的格式,可以自定義格式。萬高級自定義通過自定義,可以將文件數(shù)時(shí)間間隔等數(shù)字格式化成自己想要的格式。可以隨意自己定義語言和樣式。 hrn is short for Human Readable Number, a simple javascript for browserjs / nodejs library to format number ...

    mylxsw 評論0 收藏0
  • python深度神經(jīng)網(wǎng)絡(luò)tensorflow練習(xí)好一點(diǎn)的實(shí)體模型開展圖像分類

      此篇文章主要是給大家介紹了python深度神經(jīng)網(wǎng)絡(luò)tensorflow練習(xí)好一點(diǎn)的實(shí)體模型開展圖像分類實(shí)例詳細(xì)說明,感興趣的小伙伴可以參考借鑒一下,希望可以有一定的幫助,祝愿大家多多的發(fā)展,盡早漲薪。  文章正文  Google在各類圖象數(shù)據(jù)庫系統(tǒng)ImageNet上練習(xí)好啦1個(gè)Inception-v3實(shí)體模型,這一實(shí)體模型大家也可以用來進(jìn)去圖像分類?! ∠螺d地址:https://pan.bai...

    89542767 評論0 收藏0
  • PyTips 0x11 - Python 時(shí)間與日期

    摘要:項(xiàng)目地址時(shí)間和日期可能涉及到不同的時(shí)區(qū)格式,同時(shí)又經(jīng)常需要作為時(shí)間戳保存,有時(shí)候還需要進(jìn)行一些加減操作,因此處理起來通常會因?yàn)榉椒ㄌ喽鵁o從下手。中與時(shí)間和日期相關(guān)的標(biāo)準(zhǔn)庫有個(gè)和。 項(xiàng)目地址:https://git.io/pytips 時(shí)間和日期可能涉及到不同的時(shí)區(qū)、格式,同時(shí)又經(jīng)常需要作為時(shí)間戳保存,有時(shí)候還需要進(jìn)行一些加減操作,因此處理起來通常會因?yàn)榉椒ㄌ喽鵁o從下手。Python...

    2501207950 評論0 收藏0
  • Yii2 GridView的使用方法

    摘要:是實(shí)現(xiàn)網(wǎng)格視圖的小部件,一般用于報(bào)表視圖的展示。就是連續(xù)的列,主要用于網(wǎng)格的行號,屬于自增式的列。指定處理的類,必須。 Yii2 GridView是實(shí)現(xiàn)yii網(wǎng)格視圖的小部件,一般用于報(bào)表視圖的展示。今天,結(jié)合DataProvider(ArrayDataProvider以及SqlDataProvider)說一下GridView中的幾個(gè)Columns(SerialColumn,DataC...

    Paul_King 評論0 收藏0
  • Python 2.7終結(jié)于7個(gè)月后,這是你需要了解的3.X炫酷新特性

    摘要:截止到月號上午點(diǎn),將終結(jié)于在這一段時(shí)間中,很多優(yōu)秀開源項(xiàng)目與庫已經(jīng)停止了對的支持。除了,還提供了一種通過進(jìn)行字符串插入的靈活方法。擴(kuò)展的可迭代對象解包最低版本為對于這個(gè)特性,代碼就說明了一切。從 3.0 到 3.8,Python 3 已經(jīng)更新了一波又一波,但似乎我們用起來和 2.7 沒有太大區(qū)別?以前該怎么寫 2.7 的代碼現(xiàn)在就怎么寫,只不過少數(shù)表達(dá)方式變了而已。在這篇文章中,作者介紹了 ...

    chadLi 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<