[Codewars] 009: Weight for weight

題目

(5kyu) My friend John and I are members of the "Fat to Fit Club (FFC)". John is worried because each month a list with the weights of members is published and each month he is the last on the list which means he is the heaviest.
I am the one who establishes the list so I told him: "Don't worry any more, I will modify the order of the list". It was decided to attribute a "weight" to numbers. The weight of a number will be from now on the sum of its digits.
For example 99 will have "weight" 18, 100 will have "weight" 1 so in the list 100 will come before 99. Given a string with the weights of FFC members in normal order can you give this string ordered by "weights" of these numbers?
Example:
"56 65 74 100 99 68 86 180 90" ordered by numbers weights becomes: "100 180 90 56 65 74 68 86 99"
When two numbers have the same "weight", let us class them as if they were strings (alphabetical ordering) and not numbers: 100 is before 180 because its "weight" (1) is less than the one of 180 (9) and 180 is before 90 since, having the same "weight" (9), it comes before as a string.
All numbers in the list are positive numbers and the list can be empty.

我的答案

from functools import reduce
def order_weight(strng):
    if not strng:
        return ''
    else:
        l = strng.split()
        l.sort()
        d_l = []
        for each in l:
            num = reduce(lambda x,y: int(x)+int(y), list(each))
            if type(num) == str:
                num = int(num)
            d_l.append([each, num])
        d_lord = sorted(d_l, key=lambda x:x[1])
        d_lord = [i[0] for i in d_lord]
        return ' '.join(d_lord)

其他精彩答案

def order_weight(_str):
    return ' '.join(sorted(sorted(_str.split(' ')), key=lambda x: sum(int(c) for c in x)))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末熙宇,一起剝皮案震驚了整個濱河市硼瓣,隨后出現(xiàn)的幾起案子狈邑,更是在濱河造成了極大的恐慌,老刑警劉巖驹溃,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件垄分,死亡現(xiàn)場離奇詭異,居然都是意外死亡计福,警方通過查閱死者的電腦和手機跌捆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來象颖,“玉大人佩厚,你說我怎么就攤上這事∷刀” “怎么了抄瓦?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長克蚂。 經(jīng)常有香客問我闺鲸,道長,這世上最難降的妖魔是什么埃叭? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任摸恍,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘立镶。我一直安慰自己壁袄,他們只是感情好,可當我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布媚媒。 她就那樣靜靜地躺著嗜逻,像睡著了一般。 火紅的嫁衣襯著肌膚如雪缭召。 梳的紋絲不亂的頭發(fā)上栈顷,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天,我揣著相機與錄音嵌巷,去河邊找鬼萄凤。 笑死,一個胖子當著我的面吹牛搪哪,可吹牛的內(nèi)容都是我干的靡努。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼晓折,長吁一口氣:“原來是場噩夢啊……” “哼惑朦!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起漓概,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤漾月,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后胃珍,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體栅屏,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年堂鲜,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片护奈。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出营密,到底是詐尸還是另有隱情南缓,我是刑警寧澤,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布厌秒,位于F島的核電站读拆,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏鸵闪。R本人自食惡果不足惜檐晕,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辟灰,春花似錦个榕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至继控,卻和暖如春械馆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背武通。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工霹崎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人厅须。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓仿畸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親朗和。 傳聞我的和親對象是個殘疾皇子错沽,可洞房花燭夜當晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容