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)returnprint(targetNumber)printPattern(targetNumber - 5)print(targetNumber)# Driver Programn = 10printPattern(n)
On first glance, we notice that the targetNumber
is decreased by 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: ...