This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Anton Lodder
almost 12 years

# Different approach to digit summing

I am quite proud of my solution to digit_sum(x)

``````def digit_sum(x):
numString = str(x)
sum=0;
for digit in numString:
if digit.isdigit():
sum += int(digit)
return sum
``````

This turns the number into a string, and then you can iterate through the string and convert each letter back to a number and sum. It also works if there is a minus sign or decimal because it uses pythonβs built-in function .isdigit() to check if the character is in fact a digit

Have you seen this thread? There are some very neat solutions for this task. My favourite, because itβs so short and so βPythonyβ:

``````return sum( map( int, list( str(x) ) ) )
``````

This one doesnβt do checking, of course. Your solution has the benefit of accepting both numbers and strings (and the strings can even contain non-digits). Nice!

points
Submitted by Alex J
almost 12 years

Scott Slocum over 11 years

Iβm intrigued. I am willing to submit to the reality that this solution will make sense to me as I keep studying, but, if you could point me in the right direction, Iβd appreciate it: what is this map()?

Edditoria ζθΏͺ over 11 years

OMG! So smart! BTW, is your solution more efficient to run? I usually avoid this style if possible, because I bet I canβt read it in next morning (Iβm a noob!). But I would force myself to use this approach if it is more efficient :)

jb over 11 years

Very clever and a kind of coding style I really enjoy. However: from myβadmittedly limitedβunderstanding, this couldnβt be any less βPythonyβ. If you enter βimport thisβ into the interpreter, youβll see that this code violates several of the βZen of Pythonβ precepts, such as: β

• Flat is better than nested.
• Sparse is better than dense.
• Readability counts. β β¦and arguably a few more. I like dense code thatβs challenging to parse; I just donβt think the Python community, as a rule, does. Now, old-school C programmersβ¦.

Happy coding!

1 vote

I had the same approach, but didnβt think of using isdigit(), well done. I couldnβt think of an easy way to do it as an integer because without a len() how could the for loop be executed. Then I thought of using a while but it just seemed even more complex.

points
Submitted by Kevin
over 11 years

1 vote

``````return sum(int(i) for i in str(x))
``````
points
Submitted by Nicholas Fose

1 vote

``````def digit_sum(n):
n = str(n)
total_count = 0
for i in range(len(n)):
total_count += int(n[i])
``````
points
Submitted by Tuomas Taini
over 10 years

1 vote

I came up with:

``````def digit_sum(n):
total = 0
for i in str(n):
total += int(i)
``````
points
Submitted by Nick Kirby
over 9 years

I was gonna write about the same kind of solution, which also came natural to me.

points
Submitted by Fabrizio Bianchi
over 11 years

def digit_sum(n): Total = 0 for i in n: Total = Total + int(i) return Total

print digit_sum(β2213β)

points
Submitted by eltond
over 10 years