# 4. Digit_sum simplest solution

Spoiler: working solution!
I was trying to make the simplest and shortest form of it and I came up with this, what do you guys think? Is there a simpler option still?

def digit_sum(n):
string = list(str(n))
summa = 0
for c in string:
calc = int(c)
summa += calc
return summa

Thanks ^^

9 Likes

You can actually remove a line by compacting your for loop into:

summa += int(c)
7 Likes

@lotsoflore
Yup there is a shorter solution, not necessarily simpler.

EDIT: For Clarity

def return_sum_of_digits(number):
return sum([int(digit) for digit in str(number)])
14 Likes

Thatâ€™s right!
Thank you

1 Like

hey there,

def digit_sum(n):
ln=str(n)
summ = 0
for l in ln:
num = int(l)
summ += num
print summ
return summ

This is what I got. It isnâ€™t too much shorter, but it doesnâ€™t look like â€ślistâ€ť in list(str(n)). Is necessary.

10 Likes

I have this:

def digit_sum(n):
return sum([int(x) for x in str(n)])

If we write this with two for loops, it might be more readable:

def digit_sum(n):
#define empty list, where we will store each item of string n as number
## code "numbers_list = int(x) for x in str(n)" # does the same, just in one line, without extra variable
numbers_list = []
for x in str(n):
numbers_list.append(int(x))
#now define zero int total, and add each int from numbers_list to total
## code "sum(numbers_list)" # does the same
total = 0
for number in numbers_list:
total += number

Googled python convert int to list: http://stackoverflow.com/questions/13905936/converting-integer-to-list-in-python

and I just noticed @zeziba has the same, maybe the explanation will be useful for someone

12 Likes

You folks are amazing! Trying to learn this stuff at 43â€¦frankly, it makes my brain hurt. Iâ€™ve been at this challenge for at least a week and while I can understand the objective and hash it out in psuedo, crunching it down to two lines of code feels like an impossible feat. Iâ€™m not worthy!!!

8 Likes

Itâ€™s shorter
def digit_sum(n):
n=str(n)
c=0
for i in n:
c+=int(i)
return c
But function generators works faster and takes less space in your program.

3 Likes

I came up with this simple solution too, it doesnt overcomplicate the problem and uses less code.

def digit_sum(n):
number = str(n)
total = 0
for num in number:
total += int(num)

casting is fun!

def digit_sum(n):
result = 0
for i in str(n):
result += int(i)
return result
14 Likes

def digit_sum(n):
result = 0
for i in str(n):
result += int(n)
return result
Ran your code and got this
Your function fails on digit_sum(434). It returns 1302 when it should return 11.

I see my error, I will run it again.

Thanks for the simple easy to understand code.
Works great. Strange error.

Hey dangerdes,
I know the feeling. Iâ€™m trying to learn this at 58. Iâ€™ve played around with DOS, Visual Basic, Qbasic and others in the past, so I know some of the logic, but the syntax still bites me at times. I am FAR removed from being able to do this in just a couple of lines of code, so donâ€™t feel all alone.

2 Likes

After reading your replies (thank you very much by the way), and going forward in the course, Iâ€™m just amazed at the power of a programming language!
And to think that Iâ€™ve only seen the tip of the icebergâ€¦!

can you explain the logic behind this solution didnt understand how this statement works
return sum([int(x) for x in str(a)])

Well letâ€™s break it down,

def return_sum_of_digits(a):
return sum([int(x) for x in str(a)])

The first part of this function is the fact we are using list comprehension to generate a list.

[int(x) for x in str(a)]

Letâ€™s rename it to what it is working on

[int(digit) for digit in str(number)]

When we convert the number into a string it will be able to iterate over every digit of the number. Then we convert every digit back to an itn type when it gets placed back into the list.

After it generates the list we use the built-in function sum() to calculate the sum of all the digits in the list and return that.

It may look like gibberish but list comprehension is one of the most useful things to get line count down and it can be tons faster than loops because it is automagically converted into byte-code which runs way faster.

8 Likes

Hi What does this line mean? Thanks

Wow, thereâ€™s a variety of ways to get digit_sum function done. Here is my way which worked:

def digit_sum(n):
store = str(raw_input("Enter your numbers now then press return"))
store = list(str(n))
value_store = 0
for c in store:
value_store += int(c)
return value_store
2 Likes

I went for the challenge in the help section, doing it without flipping back & forth between str/int. Itâ€™s certainly not the shortest, but does illustrate the variety of solutions I guess.

def digit_sum(n):
number_of_digits = len(str(n))
total = 0
i = 0
while i < number_of_digits:
total = total + (n % 10)
n = n // 10
i += 1