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
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
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!!!
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.
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.
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.
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…!
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.
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
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