Recursion wont work digit_sum


#1



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

The code is ok i checked it i used recursion iand your test samples arent working but mine are working fine when i enter your numbers

It returns the coreect value but it says it isnt


sum1 = 0;

def digit_sum(x):
    global sum1
    if x <= 0:
        return sum1
  #  print sum1
    sum1 += x % 10
    return digit_sum(x/10)

#x = digit_sum(434)

#print x


#2

Your function has no reason to store any data outside of itself.

Your bug is coming from that you are changing the value of sum1, so next time you use your function it will no longer start at 0. Again, your function should not be storing any information outside of itself.

Your function should be communicating with itself only through arguments and return values.


#3

Yeah i know wasnt trying to act smart but i cant add one more parrameter because your test examples wont work since they are made to pass one variable in this case x so if i add a sum parameter it will still come as wrong, but i still get the right values when i manually test thats what i was wondering why it wasnt working


#4

You can add more parameters. But you shouldn't be doing that.

As I said, you won't get the right values if you call your function multiple times.

The test data is good. Your function is not correct.

The way to solve this is:

The sum of the digits is: the first digit + the sum of the remaining digits (call self to obtain this)


#5

I guess i can make it work like
if x <= 0:
tmp = sum1
sum1 = 0
return tmp
but its just being complicated :D, and i didnt say your test examples werent working had no intention of being a smartass although it sounded like that, waas just curious


#6

It's fully possible to do it with a single parameter and no outside storage. The last line of my previous response says how to do it! Just not in code.


#7

Yeah i know thanks for the fast reply i was just being curious wanted to experiment in the end i did it with a simple while just wanted to descuss the matter. Thanks :slight_smile:


#8

You're so close to a recursive solution though..

The idea of recursion is to make the problem slightly smaller (take the first digit) and solve the rest (call self with the remaining digits) and then assemble the result (add the first digit with the sum of the other digits)

The problem is reduced slightly each time the function calls itself. Eventually it gets to the point where the problem is super trivial (no remaining digits) which is handled by a special case (if no digits: return sum of 0) which then allows the previous calls which have so far been unable to finish, to get the information they need and return.


#9

Yeah i knew there was like a super simpler solution but i always had trouble understanding it even when i passed my c exam in recursion used this amateur solution cause i don't know how to save the sum in the new call of the function without adding a global variable or another parameter. Since variables in a function are local so when you call it again they are reset thats what i understood. Sorry if im talking nonsense :smiley:


#10

Hey @thadevil666 ,

Using a recursion for this problem was a great idea, so don't give up on it.


#11

Somehow, my reply got marked as the solution. Well, not giving up is part of the solution, but the real solution is the algorithm itself.


#12

Im sorry about that wanted to like your comment heh i was silly and misclicked :slight_smile:


#13

I think it's worth giving it some more time. And it really is very simple, if a bit mind-boggling at first.

Convince yourself of that this is correct:

If you have the first digit and the sum of the rest, then you can solve it.
The sum of the rest can be obtained by calling self.

Along with:
It will always return because it's reduced until trivial and the trivial case gets solved.


#14

You can think of it in terms of what the result IS.
Forget about how it's actually computed.

The digit sum of no digits IS 0
The digit sum of digits IS the first plus the sum of the rest.


#15
def digit_sum(x):
    if x <= 0:        
        return x 
    else:
        return x%10 + digit_sum(x/10)

Oh my god i actually understand it now it keeps the x % 10 like sepparate from the function untill it ends so it adds up until the function ends its like summing 2 variables all this time i was confused cause int + function looked so complex to me now i actually understand i was looking at it liek it was a parrameter when it actually isnt. Thank you man thanks for the support and sorry for wasting your time with silly problems :slight_smile: