Reputation: 15
I have been trying to pass value from my recursive helper function to the parent, but it is passing None
. I am learning Python and recursion and trying to understand how the values are passed between functions.
def superDigitSum(n,k):
num2str = [int(i) for i in list(str(n))*k] #creates the list[1,4,8,1,4,8,1,4,8]
result=0
return superDigitSumHelper(num2str, result)
def superDigitSumHelper(num2str, helper_result):
if not num2str :
return helper_result
else:
helper_result+=num2str[-1]
superDigitSumHelper(num2str[:-1],helper_result)
print(superDigitSum(148,3))
The code creates the list [1,4,8,1,4,8,1,4,8] and sums the values. I expect the result to be 39
(3 * (1+4+8)).
Upvotes: 0
Views: 749
Reputation: 135227
If you're allowed to do things like str(n)
or list*k
, or even *
at all, it kind of defeats the purpose of the exercise
def super_digit_sum (n, k):
return sum (map (int, list (str (n)))) * k
print (super_digit_sum (148, 3)) # 39
But I'm sure the point of this is to use primitive features and operations as a means of learning how to write simple recursive programs...
We start with a simple function that sums the digits of a single input number
def sum_digits (n):
if n < 10:
return n
else:
return n % 10 + sum_digits (n // 10)
print (sum_digits (148))
# 13
Then we make a simple function that can multiply two numbers
def mult (n, m):
if n is 0:
return 0
else:
return m + mult (n - 1, m)
print (mult (3, 7))
# 21
Then combining the two, we write your main function
def super_digit_sum (n, k):
return mult (sum_digits (n), k)
print (super_digit_sum (148, 3))
# 39
If you're allowed to use *
, you can skip mult
altogether and simply write
def super_digit_sum (n, k):
return sum_digits (n) * k
print (super_digit_sum (148, 3))
# 39
This is a better program because you arrive at the answer in a straightforward manner. Compare that to the other approach of taking a number, converting it into a string, chopping it up into single characters, converting those characters back to digits, then summing the digits, and finally multiplying the result by a constant.
Both approaches arrive at the same result, but only one results in a program worth reading, imo. I talk about this roundabout-style of programming in another python Q&A: How to count neighbors in a list? I think you will find it helpful.
Upvotes: 0
Reputation: 77847
This clause returns None
to the next call up the stack. You need to return the last value:
else:
helper_result+=num2str[-1]
superDigitSumHelper(num2str[:-1],helper_result)
Instead,
else:
helper_result+=num2str[-1]
return superDigitSumHelper(num2str[:-1],helper_result)
Upvotes: 1
Reputation: 10704
Your function is slightly wrong.
You need to add a return the following to your superDigitSumHelper function:
return superDigitSumHelper(num2str[:-1], helper_result)
in your else conditional.
Essentially, it is returning none because after the function ends, it isnt returning anything so the default value is none.
Upvotes: 1
Reputation: 1552
try
else:
helper_result+=num2str[-1]
return superDigitSumHelper(num2str[:-1],helper_result)
Without returning the results of the recursive call you are just returning None
Upvotes: 1