WHEN TO USE RECURSION?
The subproblems are the same as the bigger problem
Problems which require an arbitrary number of nested loops.
solving the problem via the solution of the smaller version of the same problem
Break down a complex problem into a simpler one
Solve the simpler one, and then use that result to go up and solve the bigger problem.
Recursive tree is often useful
Solving a problem with recursion:
# Example:
def is_in_list(num, lst):
""" Find out if the number is in the list recursively """
if len(lst) == 0:
return False
else:
return num == lst[0] or is_in_list(num, lst[1:])
Quick Summary: