国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

【python】Human readable duration format 時間格式轉換

liangzai_cool / 2922人閱讀

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

背景

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

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/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元查看
<