...

/

Understanding a Recursive Problem

Understanding a Recursive Problem

In this lesson, we will go over methods to help you visualize a recursive function.

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:

Press + to interact
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 ...