0

I am a beginner, trying to learn recursion and solve some problems (trying the subsequences problem). But before I could even attempt to get the recursion logic right, I am getting stumped while trying to store the values returned. Here is what I tried and the outputs I received. Experimented putting some print commands just to understand. I thought the following will give me answer = [[b,c],[c]] instead, it appears the stored value is "None". Hope someone can explain what I am doing wrong and how to correct this, so that I can then proceed with the subsequences problem. Thank you.

Arun

def subseq(arr, answer=[""]):
    if len(arr) == 0:
        return("")
    print("arr", arr)
    answer += subseq(arr[1:],answer)
    print("answer", answer)
arr = ['a','b','c']
subseq(arr)

#--------------------------------------------------------------------

I was hoing to get ['b','c'] and ['c'] as the answer but can't get that. Output is as follows: arr ['a', 'b', 'c'] arr ['b', 'c'] arr ['c'] answer [''] #This followed by the following error: answer += subseq(arr[1:],answer) #TypeError: 'NoneType' object is not iterable

Rob
  • 14,746
  • 28
  • 47
  • 65
ArNY
  • 65
  • 1
  • 9
  • 1
    You only `return` in your base case. You need to return something in the recursive case too, or you return `None` by default. I'd also note that you'll have a problem with this code if you call it multiple times, as the `answer` list is going to be shared by all calls. – Blckknght May 06 '22 at 22:31
  • How do I do that in this case? Once I say return, wouldn't the recursive code get terminated? I was hoping to get answer = '[b,c]; [c] – ArNY May 06 '22 at 22:39
  • @ArNY Once you say return, the specific function call gets terminated. Not all of the function call gets terminated in a single go. In other words, doing return once terminates one function call of the function/method. – Feroz Ahmad May 06 '22 at 22:49

1 Answers1

0

Your code has other bugs as well.

Think of the recursion as:

  1. What do you want to do in each recursion step
  2. How does your input becomes smaller than before after doing the operation in step 1.
  3. Doing recursion on a smaller part of input obtained in step 2.
  4. Think about the base case where recursion needs to stop and put it at the top.

Thus, you need to modify your code in the following way. Please follow my comments.

def subseq(arr, answer):
    # Step 4: Base case
    if len(arr) == 0:
        return

    print("arr ", arr)

    # Step 1: Think about what you want to solve in each step
    subsequence = arr[1:]
    
    # collect your operation output in answer
    if len(subsequence) > 0:
        answer.append(subsequence)
    
    # Step 2: New input
    new_arr = arr[1:]  # should become smaller at each step


    # Step 3: Make more recursion
    subseq(new_arr, answer)

arr = ['a','b','c']
answer = []  # Allocate memory for storing answer before hand
subseq(arr, answer)
print("answer", answer)
Feroz Ahmad
  • 122
  • 2
  • 7
  • Thank you so much. I am going through your comments. Hopefully I will get the concept. – ArNY May 06 '22 at 22:47
  • Thank you. I saw all my mistakes (including some silly ones like assuming + between arrays will be the same as append. really appreciated your help. – ArNY May 06 '22 at 23:59
  • I would suggest you (@ArNY) check out other simpler problems using recursion first, for example, factorial using recursion, and string reversal using recursion. These two problems help you absorb the concept of recursion better, – Feroz Ahmad May 08 '22 at 19:15
  • For some reason those two problems did not pose any difficulty. Maybe sometimes I over think and complicate things. I don't know. def reverse(str): if len(str) == 1: return (str) else: return(str[-1]+ reverse(str[:-1])) ## str="abcde" print(reverse(str)) – ArNY May 08 '22 at 20:42
  • Can't say if factorial would have caused a problem because that was the example used to introduce recursion, so I never had to solve it. Hopefully I will get it one day. – ArNY May 08 '22 at 20:50