0

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)).

3
  • 1
    If you have an answer which suits your needs, please dont forget to mark it as such for people to later use as a point of reference. :) Commented Jun 7, 2018 at 17:14
  • Python programmers use snake_case instead of lowerCamelCase. You should name your function super_digit_sum instead of superDigitSum. Commented Jun 7, 2018 at 19:01
  • When you get to a resolution, please remember to up-vote useful things and accept your favourite answer (even if you have to write it yourself), so Stack Overflow can properly archive the question. Commented Jun 7, 2018 at 21:07

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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)

Comments

0

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.

1 Comment

Sure thing. I will do my best to use primitive features to solve the exercises. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.