Order by sum of digits X35790


Statement
 

pdf   zip

html

Write a function sum_of_digits_sorted(f) that provided a list of non negative integers f returns a list g with the very same numbers in f. Numbers in g must appear in increasing sum of digits. For instance, if 56 and 2131 are numbers in f, number 2131 will appear before 56 in g because the sum of digits of 2131 (sum is 7) is less the sum of digits of 56 (sum is 11). Numbers of the same sum of digits must appear in the usual increasing order.

Sample session
>>> sum_of_digits_sorted([56, 2131])
[2131, 56]
>>> sum_of_digits_sorted([313, 44, 36, 11111, 35, 26, 7])
[11111, 7, 313, 26, 35, 44, 36]
>>> sum_of_digits_sorted([9, 53, 511, 4000, 10001, 45])
[10001, 4000, 511, 53, 9, 45]
Information
Author
ProAl
Language
English
Official solutions
Python
User solutions
Python