-
Notifications
You must be signed in to change notification settings - Fork 0
/
weight-for-weight.py
39 lines (32 loc) · 1.2 KB
/
weight-for-weight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Solution to the CodeWars kata Weight for weight
Link:
https://www.codewars.com/kata/55c6126177c9441a570000cc/python
"""
def order_weight(strng):
#Function that orders a string of numbers separated by space by the sum of its digits. If two numbers sum the same, consider them strings, the shortest one goes first.
result=[]
answer=[]
numbers=sorted(strng.split())
for item in numbers:
weight=0
for ch in item:
weight+=int(ch)
result.append([weight,item])
result.sort()
for item in result:
answer.append(str(item[1]))
return " ".join(answer)
# --------------------------------------------Extras--------------------------------------------
#Other solutions
def order_weight2(strng):
#Function that orders a string of numbers separated by space by the sum of its digits. If two numbers sum the same, consider them strings, the shortest one goes first.
initial_list = sorted(strng.split())
result = " ".join(sorted(initial_list, key=sum_string))
return result
def sum_string(s):
#Function used in order_weight2
sum = 0
for digit in s:
sum += int(digit)
return sum