Digit_Sum


#1

Hello,

The correct code to get the digit sum is below

def digit_sum(x):
    total = 0
    while x > 0:
      total += x % 10
      x = x // 10
      print x
    return total

However, if I modify the code to be

def digit_sum(x):
    total = 0
    while x > 0:
      x = x // 10
      total += x % 10
      print x
    return total

It is wrong. Why is that?

Lets assume x = 25
then x = x//10 = 2 (first digit)
then total += x%10 = 5 (second digit)

How does the sequence of the code work?

Please help


#2

It should work for all numbers, not just 25 (and working for 25 doesn’t mean it works for all numbers)
And besides, you don’t keep the 2, so it won’t work for 25 either

You should write the code in a way that it is convincing that it’ll behave correctly for all numbers

Most likely this means doing one digit per iteration in your loop, rather than doing both 2 and 5 (in the case of 25) during the first iteration

I suggest executing your code manually and observe what you end up doing.
I also suggest observing how you would do the task manually, and then model your code after that so that the code does the same thing that you’d do.


#3

Hello there… I broke my head on how to do this one as well… but your code seems so confusing I barely understand what does it mean…
what I did to make sure it’ll work with any positive number… is to simply change the number into a string… (because the FOR function reads every letter if the value is a string… )
then I changed it back to integer when I summed it up with my “total”


#4

The string conversion from int necessarily has to do the same thing, so it is arguably more complicated (does the same steps and more) - but matches how a human would do it, we read one digit at a time, not the whole number and do division

It’s very simple though, just consider what happens when you divide by 10 without keeping decimals, and what happens when you multiply it by 10 again and subtract from the original (same as computing remainder)


#5

ahh… I’m confused… tried to read his code for the third time… clueless still…

def digit_sum(n):
  result = 0
  for number in str(n):
    result += int(number)
  return result

well… this is my code…


#6

Try executing it manually and observe what you end up doing.
You should know what each operation does there anyway


#7

I got it now…
in my opinion it is very different from my approach… and seems like a more complicated method to think about…

now you said that my method is more complicated because it takes more steps… turning it into a string… and than back into integer…
does it mean it’ll take more computational power?
may be a stupid question… but i’m genuinely interested


#8

When you do str(number), the string conversion has to divide by 10 to pop off the digits and produce the string

So it does the same, plus some more

So yes, more computing, but it’s insignificant (more but not in a way that matters)

Personally I’d do it like you:

sum(map(int, str(1234)))  # 10

#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.