2

I've been trying to solve a problem which asks for a function which returns the question of the title. One example of the output would be the following one:

sum_of_digits_sorted([56, 2131])

returns

[2131, 56]

Since 2+1+3+1 < than 5+6, the new list would be sorted as we can see in the example.

I have used a code that iterates on the list and also coded a way to sum digits but I dont know how to apply them together

PD: I'm new in this web so I do not know how to attach my programs and that stuff. Thank you!

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • what is your effort so far? – Rarblack Nov 26 '18 at 10:59
  • Welcome to SO. No need to attach, you can copy-paste your code into the box. Please do so we can help you with the last step! – Josh Friedlander Nov 26 '18 at 11:05
  • To sum the digits, you can use any of these methods: https://stackoverflow.com/questions/14939953/sum-the-digits-of-a-number-python and to sort according to those, just use the list.sort(key=...) argument to provide your digit summing function. If you have difficulties with that, we can help you. – Eric Darchis Nov 26 '18 at 11:11
  • `def sum_of_digits_sorted(l):return sorted(l, key = lambda x: sum(map(int,str(x))))` would be the application of what @EricDarchis suggested – Patrick Artner Nov 26 '18 at 11:15

3 Answers3

0

You can try this:

In [553]: l
Out[553]: [56, 2131]

In [554]: new = []  # define an empty list

In [555]: for c,i in enumerate(l):
     ...:     if c < len(l)-1:
     ...:         lst = map(int, str(l[c])) # converts individual element into a list
     ...:         lst1 = map(int, str(l[c+1]))
     ...:         if sum(lst) > sum(lst1): # sum(lst) gives the sum of the list
     ...:             new.append(l[c+1])
     ...:             new.append(l[c])
     ...:         else:
     ...:             new.append(l[c])
     ...:             new.append(l[c+1])
     ...:         
     ...:     

In [556]: new  # will be a sorted list based on the sum of digits of individual elements.
Out[556]: [2131, 56]
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
0

This is only for a demonstration of the idea, it will calculate the sum for every comparison, which is not very effective for a large list:

def sum_of_digits_sorted(a):
  return sorted(a,key=lambda x: sum([int(i) for i in str(x)]))

>>> sum_of_digits_sorted([1, 2131, 56, 99])
[1, 2131, 56, 99]

This is the same idea with a temporary tuple with pre-calculated keys:

list(map(lambda y:y[1],sorted([(sum([int(i) for i in str(x)]),x) for x in a],key=lambda x: x[0])))

There is a comparison of different methods of calculating sum of decimals and when I compared this method above with the fastest one from Sum the digits of a number - python this one is about 30% slower which should not be a problem unless you calculate a really huge number of sums. Even though it is like 3 times slower in that answer. For a one-time script probably anything will do. For a more speed-critical thing I would went with the fastest one available - possibly tested on a comparable hardware and python version.

petrch
  • 1,807
  • 15
  • 19
0

You can use an algorithm to calculate the digital root of an integer via sum and a generator expression. Then use this as a sort key:

from math import log10

L = [56, 2131]

def sum_of_digits_sorted(L):
    def summer(x):
        return sum(x // 10**i % 10 for i in range(int(log10(x))+1))
    return sorted(L, key=summer)

print(sum_of_digits_sorted(L))

[2131, 56]
jpp
  • 159,742
  • 34
  • 281
  • 339