Recursion Review - Depth First Search / Introduction

https://algo.monster/problems/recursion_intro

Dennis answers Abdul’s call and says great but he also wants to meet Carly, so he puts Dennis on hold and calls Carly.

Incorrect. Dennis puts Abdul on hold not himself.

Error:
def factorial_stack(n):
stack = []
# push each call to a stack
# top of the stack is base case
while n > 0:
stack.append(n)
n -= 1

res = 1
# pop and use return value until stack is empty
while stack is not None:
    res *= stack.pop()

return res

Might need to use something like instead of None:

def factorial_stack(n):
stack = []
# push each call to a stack
# top of the stack is base case
while n > 0:
stack.append(n)
n -= 1

res = 1
# pop and use return value until stack is empty
while stack is not None:
    res *= stack.pop()

return res

factorial_stack(n) ends in an error - IndexError: pop from empty list.

What a cool example of real-life recursion!

1 Like

It’s amazing how powerful a visualizations are when explaining concepts like this. Got a good ways before I am at this section but wanted to take a peek. :stuck_out_tongue:

Make the image with better quality please hahahaha

1 Like

are you trying to read the text in the cartoon? haha

Expanding the image on click would be great! It’s readable after browser zoom but not the best user experience

Really love the visualizations! Both the real comic example and the call stack example. I’ve studied recursion before but this is one of the best ways I’ve seen it taught, especially for newbies.