-2

I have a code that works however I do not fully understand how this line works:

return (myFunc%10) + numbersum(myFunc//10)

Why does this line calculate the sum of the digits?

Here is the full code

def numbersum(myFunc):
    if myFunc == 0:
        return 0
    else:
        return (myFunc%10) + numbersum(myFunc//10)

Any help would be greatly appreciated.

brianpck
  • 8,084
  • 1
  • 22
  • 33
Alex Chase
  • 59
  • 5
  • 5
    This is too broad. What don't you understand? Recursion? Mod? Division? Addition? Checking for equality? – Morgan Thrapp Nov 02 '16 at 17:32
  • "Why does this line calculate the sum of the digits?" That's the purpose of the function, so sum the digits of a number. – Patrick Haugh Nov 02 '16 at 17:34
  • 1
    Changing `myFunc` to `number` is a step in the right direction – brianpck Nov 02 '16 at 17:34
  • Usign a variable name of `myFunc` for something that is a number does not helping understanding but add `print(myFunc%10, myFunc//10)` before the return line and you should start getting an idea of how this works. – Ma0 Nov 02 '16 at 17:35

3 Answers3

3

I'm not sure which part of the line you don't understand since you didn't specify, but I'm going to assume it's one of:

  1. modulo operator (%);
  2. integer division operator (//); or
  3. recursion

(1) is easily googled, and there are plenty of good explanations (for example, the first line in Wikipedia article here): "In computing, the modulo operation finds the remainder after division of one number by another (sometimes called modulus)."

(2) See this stack overflow post for an explanation of the // operator.

(3) A function calling itself, as this one does, is an example of recursion, and the function is said to be a "recursive function." A key component of a recursive function is the "base case," a case where the function does not call itself. Without a base case calling the function once would enter into an infinite loop where the function calls itself forever. The if-statement in this code defines a base case.

A common pattern in recursion is, on the recursive call, to pass to the function an argument that is "simpler" than the argument it received (in that it is "closer" to the base case).

Let's run through a quick example. If I call numbersum(123):

  1. Is 123 == 0? No. So, numbersum(123) equals 123%10, which is 3, plus numbersum(123//10), which is numbersum(12).
  2. Now we have to figure out numbersum(12). Same steps. Is 12==0? No. So, numbersum(12) equals 12%10, which is 2, plus numbersum(12//10), which is numbersum(1).
  3. So what is numbersum(1)? Same steps. Is 1==0? No. So, numbersum(1) equals 1%%10, which is 0, plus numbersum(1//10), which is numbersum(0).
  4. Lastly, what is numbersum(0)? Is 0==0? Yes! The base case is hit here. So numbersum(0) equals 0.

If you look back through these steps, you will see that

numbersum(123) = 3 + numbersum(12) numbersum(12) = 2 + numbersum(1) numbersum(1) = 1 + numbersum(0) numbersum(0) = 0

Working youre way back up the chain using basic algebraic substitution, you get the correct answer: 6.

Community
  • 1
  • 1
1

If you have the number 123 stored in myFunc then myFunc%10 is going to get you 3 because 123%10 == 3, you then do myFunc//10 which gets you 12. you pass this to your function which is where the recursion happens. So you end up with 3 + The result of your function being called on 12. This repeats until you have summed all the digits.

bravosierra99
  • 1,331
  • 11
  • 23
0
  • (myFunc%10) mod 10 - Takes the one's digit
  • numbersum(myFunc//10) recursively calls the function, dividing the number by 10, therefore "chopping off" the ones digit.
  • Until... myFunc == 0 (which really should be <=), in which case 0 is returned, added to the previous ones digit from the call "up the stack"
  • All numbers summed back up the call stack until you have added all the numbers
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245