Profile image of anjoman
Submitted by anjoman
over 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

Answer 5097dfb45a49da0200000ad7

3 votes

Permalink

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!

Profile image of fanaugen
Submitted by fanaugen
over 12 years

3 comments

Profile image of smslocum
Submitted by smslocum
about 12 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()?

Profile image of Edditoria
Submitted by Edditoria
about 12 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 :)

Profile image of jdburns
Submitted by jdburns
about 12 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!

Answer 50a3ab5a76b0f32f25003fb0

1 vote

Permalink

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.

Profile image of kevn57
Submitted by kevn57
over 12 years

Answer 51c55ee49c4e9d4105009c2e

1 vote

Permalink

return sum(int(i) for i in str(x))
Profile image of sohjee
Submitted by sohjee
over 11 years

Answer 52d59375282ae3a99c000260

1 vote

Permalink

def digit_sum(n):
  n = str(n)
  total_count = 0
  for i in range(len(n)):
    total_count += int(n[i]) 
  return total_count
Profile image of tagMaster07934
Submitted by tagMaster07934
about 11 years

Answer 54f0552e86f552e543001775

1 vote

Permalink

I came up with:

def digit_sum(n):
    total = 0
    for i in str(n):
        total += int(i)
    return total  
Profile image of pyJumper07058
Submitted by pyJumper07058
almost 10 years

Answer 509b5dc6da4f7d0200000513

0 votes

Permalink

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

Profile image of fabriziobianchi
Submitted by fabriziobianchi
over 12 years

Answer 52ddb16e80ff33dfb60015b5

0 votes

Permalink

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

print digit_sum(“2213”)

Profile image of eltond
Submitted by eltond
about 11 years

1 comments

Profile image of eltond
Submitted by eltond
about 11 years

I get an error here…..why?