Search⌘ K

Understanding a Recursive Problem

Explore how to understand and solve recursive problems by examining base cases and recursive patterns. Learn to visualize recursive calls using stacks and trees, and track variables to dry run your code effectively.

Understanding the Problem

In the previous lessons, we learned the basic concept of recursion and its uses. Now, we will discuss how recursion works.

Let’s take a look at an example code:

Python 3.5
def printPattern(targetNumber) :
if (targetNumber <= 0) :
print(targetNumber)
return
print(targetNumber)
printPattern(targetNumber - 5)
print(targetNumber)
# Driver Program
n = 10
printPattern(n)

On first glance, we notice that the targetNumber is decreased by 55 and printPattern() is being called again. However, there are two print() statements preceding and succeeding the recursive call.

Code Explanation

We want to print a pattern: 10 ...