日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

[简单题]换一个思维,代码简洁度就完全变了(Python实现)

發(fā)布時間:2025/4/16 python 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [简单题]换一个思维,代码简洁度就完全变了(Python实现) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目名字:

Human readable duration format

原題鏈接:

https://www.codewars.com/kata/human-readable-duration-format/train/python

就是將一個秒鐘化為人能看懂的時間,要注意細(xì)節(jié),應(yīng)該就是可以做出來的。

以前打C++的時候做了蠻多的,不過這題好像不支持C++。

但是我還是按照類似的思路做了一遍復(fù)雜度大概也就是一個O(n)

但是代碼中有不少的重復(fù)段。所以有些長。

def format_duration(seconds):if seconds == 0:return 'now'a = [0] * 5b = ['second', 'minute', 'hour', 'day', 'year']ans = ''time = 0for i, s in enumerate(a):if i < 2:a[i] = seconds % 60seconds //= 60if a[i] == 0:continueelif a[i] == 1:if time == 0:ans = '1 '+ b[i] + anselif time == 1:ans = '1 '+ b[i] +' and '+ anselse:ans = '1 '+ b[i] +', '+ anstime += 1else:if time == 0:ans = str(a[i]) + ' ' + b[i] + 's' + anselif time == 1:ans = str(a[i]) + ' ' + b[i] + 's' + ' and ' + anselse:ans = str(a[i]) + ' ' + b[i] + 's' + ', ' + anstime += 1elif i == 2:a[i] = seconds % 24seconds //= 24if a[i] == 0:continueelif a[i] == 1:if time == 0:ans = '1 '+ b[i] + anselif time == 1:ans = '1 '+ b[i] +' and '+ anselse:ans = '1 '+ b[i] +', '+ anstime += 1else:if time == 0:ans = str(a[i]) + ' ' + b[i] + 's' + anselif time == 1:ans = str(a[i]) + ' ' + b[i] + 's' + ' and ' + anselse:ans = str(a[i]) + ' ' + b[i] + 's' + ', ' + anstime += 1elif i == 3:a[i] = seconds % 365seconds //= 365if a[i] == 0:continueelif a[i] == 1:if time == 0:ans = '1 '+ b[i] + anselif time == 1:ans = '1 '+ b[i] +' and '+ anselse:ans = '1 '+ b[i] +', '+ anstime += 1else:if time == 0:ans = str(a[i]) + ' ' + b[i] + 's' + anselif time == 1:ans = str(a[i]) + ' ' + b[i] + 's' + ' and ' + anselse:ans = str(a[i]) + ' ' + b[i] + 's' + ', ' + anstime += 1else:a[i] = secondsif a[i] == 0:continueelif a[i] == 1:if time == 0:ans = '1 '+ b[i] + anselif time == 1:ans = '1 '+ b[i] +' and '+ anselse:ans = '1 '+ b[i] +', '+ anstime += 1else:if time == 0:ans = str(a[i]) + ' ' + b[i] + 's' + anselif time == 1:ans = str(a[i]) + ' ' + b[i] + 's' + ' and ' + anselse:ans = str(a[i]) + ' ' + b[i] + 's' + ', ' + anstime += 1return ans
主要是那個判斷加's' 、','、' '、'and'?

打了沒多就可以搞定很多都是重復(fù)字段。

!但是,要注意一個細(xì)節(jié),在Python中,默認(rèn)是可以做數(shù)的標(biāo)準(zhǔn)除法的,不像是之前的C++或者C。

/表示的取整

//(in Python) == ?/ (in C/C++)

應(yīng)該看懂上面這段話是沒有問題的了。


按照套路,我們會分析一下,那些大佬的代碼

times = [("year", 365 * 24 * 60 * 60),("day", 24 * 60 * 60),("hour", 60 * 60),("minute", 60),("second", 1)]def format_duration(seconds):if not seconds:return "now"chunks = []for name, secs in times:qty = seconds // secsif qty:if qty > 1:name += "s"chunks.append(str(qty) + " " + name)seconds = seconds % secsreturn ', '.join(chunks[:-1]) + ' and ' + chunks[-1] if len(chunks) > 1 else chunks[0]


在我那時,這個解法應(yīng)該是最多認(rèn)可的。

從循環(huán)的過程來看,這個是倒著來的。(所以,他都是直接取余數(shù),而我之前那個版本是做整除)

在這個代碼中,先是顯示了一個由元組所組成的列表。可以看出,這家伙對于enumerate的理解應(yīng)該是比較高的呢。

關(guān)于函數(shù)內(nèi)部實現(xiàn),第一段的那句返回,很顯然

第二部分設(shè)置了一個空的列表,用于儲存分段的數(shù)據(jù)

在一開始的時候所設(shè)置的列表,在這里起到了作用,可以直接提取出對應(yīng)的分刻數(shù)據(jù),用于計算。也正是這個原因,他的代碼簡潔度才得到了提高。

之后,由于name 是臨時變量,所以,可以直接做+='s'處理,同樣很方便。

之后就是正常的入庫處理了。

在最后的返回的時候,做了一個判斷處理。可以看出,只有當(dāng)總數(shù)大于等于二的時候,才需要做+and處理,而大于2的部分,做加','處理,否則就直接返回第一個



總結(jié)

以上是生活随笔為你收集整理的[简单题]换一个思维,代码简洁度就完全变了(Python实现)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。