Code Wars 21 : Weight for weight


問題の説明


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?
When two numbers have the same "weight", let us class them as if they were strings (alphabetical ordering) and not numbers:
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.
サマリ
指定した文字列内の数値をウェイトに基づいてソートします.重み付けは、1つの整数からなるすべての数値の和です.(ex: 123 => 1+2+3 = 6)
重みが同じ場合は、文字列の昇順で並べ替えます.

せいげんじょうけん


None

I/O例

"56 65 74 100 99 68 86 180 90" -> "100 180 90 56 65 74 68 86 99"

に答える

def order_weight(strng):
    return " " .join(sorted(sorted(strng.split(" ")), key=lambda x: sum(map(int, x))))
これは文字列加工の問題で、ステップ・プロシージャを作成し、順番に適用するだけです.
1.問題に文字列があり、split()でリストする必要があります.
2.重みが同じ場合は、文字列の昇順で並べ替える必要があるため、リストはsorted()に並べ替えておきます.
3.問題の核心は重み付けソートです.すべてのデジタルビット数の総和(重み付け)は、sum(map(int, x))によって簡略化することができる.sortedおよびkeyによって並べ替えられる.
4.並べ替えが完了したら、文字列化し直す必要があります.書き込み" ".join()