Digit_sum


#1


https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096

These codes was written by someone here in a previous topic
could someone describe their logic?

This is the first solution code:
def digit_sum(n):
   soma = 0
   while n >= 10:  
      soma+= n % 10
      n = n//10
   else:
      soma+= n
   return soma
This is the other one:
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 



Add your code here, between the ```'s!

#3

isn't it the idea you write your own solution? Well, i do find the first solution cleverly found, not one i would choice, but still.

let's take a test case, n = 1234
now while n bigger then 10 (1234 is bigger then 10), do this while loop. But what does the while loop do? n % 10 will return the remainder, which will be a number smaller then 10, which means as longer as the number is bigger then 10, it will be the last digit, for the first run of our loop this is 4. which we are going to add to soma. on the next line we divide by 10: n = n//10, the double / will also floor the number. You can see this nicely if we add a bunch of print statements:

def digit_sum(n):
   soma = 0
   while n >= 10: 
       print n, n % 10
       soma+= n % 10
       print soma, n // 10
       n = n//10
   else:
      soma+= n
   return soma
print digit_sum(1234)

but you should really write your own solution, this are such a good challenges


#4

Reading and trying to understand other people's code is also great practice. So even that is not something that you should leave to others! Dig in.

Sometimes that means spending some time with every single part of the code.

The below code reads as: The sum of each character in the string representation of a number converted to an integer.

def digit_sum(n):
    return sum(map(int, str(n)))

What map does is apply a function (here int) to each value (each character). You probably already know what the other functions do. Example below.

>>> def add_one(x):
...     return x + 1
... 
>>> map(add_one, [3, 9, 6, 3])
[4, 10, 7, 4]

#5

This really over complicates it. Personally I used:

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

and it worked.

My code basically just converts the number to a string so that it can be counted. Then it converts it back to an integer in the loop and adds it to the total. No need to get more complicated than that.


#6

I already wrote my own code, but i try to understand how others solved the problem.
there are many solutions to this problem, but i only didn't understand these two.
thanx for your help.


#7

Thank you that was helpful.


#8

Your code is much simpler, but in the instructions or the hint it was mentioned that the code should include modulo & floor division.
thanks for response.


#9

In terms of what the cpu has to do - converting to string will involve doing the same math as a part of that process. Arguably it is therefore simpler to use arithmetic's


#10

I think in the hint it said that one way to do it was use floor division but the only requirement was to solve the problem.


#11

thanks this really made it easier.