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 :slight_smile:

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
    return total

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 :slight_smile:

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)
return total

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…!
:smiley:

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
return total
2 Likes