Heyy, how are you doing?? Welcome to another lesson from NCP. Today we will be learning about recursion and recursive algorithms. Basically, recursion is one of the many ways to solve a problem. A problem could be anything, it could be printing the lyrics to a song, adding 2 with 2 or getting some data from the web. Recursion and Iteration are the two basic ways to solve problems. We will be discussing about both of these methods in the following article.
DIFFERENCE
BETWEEN ITERATIVE ALGORITHMS AND RECURSIVE ALGORITHMS:
Iterative
algorithms solve
problems by repeating steps over and over, typically using a loop. These
algorithms rely on loops
to solve problems.
Recursion
is a method of solving problems by breaking a problem up into smaller and
smaller pieces until it can be easily solved. Recursive algorithms rely on functions that call
themselves to solve problems. Any problem you can solve iteratively can be
solved recursively.
You write a recursive
algorithm inside of a function. A function must have a base case. A base case is a condition that ends the recursive algorithm to
stop the algorithm from continuing forever.
Inside the function, the
function calls itself and each time it calls itself it moves closer to the base
case. Eventually the base case condition is satisfied, the problem is solved
and the function stops calling itself.
THE
THREE LAWS OF RECURSION:
A recursive algorithm should always
satisfy the three laws of recursion:
` 1. A recursive algorithm must have a base case
2. A recursive algorithm must change its state
and move toward the base case
3. A recursive algorithm must call itself
recursively
CONCLUSION:
A recursive algorithm can
sometimes be a great way to solve complex problems. Recursive algorithms always
follow the three laws of recursion.
Here is a fun little example
of a recursive algorithm:
def bottles_of_beer(bob):
if bob < 1:
print(“”"No
more bottles of beer on the wall. No more bottles of beer. “””)
return
tmp =
bob
bob
-= 1
print(“””{}
bottles of beer on the wall. {} bottles of beer. Take one down, pass it around,
{} bottles of beer on the wall”””.format(tmp, tmp, bob))
bottles_of_beer(bob)
bottles_of_beer(99)
Here, we are printing the
lyrics to the popular pop song "99 Bottles Of
Beer On The Wall”. Let us see
how this code follows the three laws of recursion:
After defining the function,
we set a base case for the function with the ‘if’ block. When ‘bob’ becomes less than 1, the function
return and stops calling itself.
When we decrement ‘bob’ by 1
with the line ’bob -= 1’, we
are satisfying the second law of recursion because the variable ‘bob’ moves
towards the base case every time the function calls itself.
The third and final law of
recursion is satisfied with the line ‘bottles_of_beer(bob)’
that calls the function recursively.
Here is how the final output
looks like:
Let us take a look at another
example of a recursive algorithm:
def print_to_zero(n):
if n
< 0:
return
print(n)
print_to_zero(n-1)
print_to_zero(5)
Can you see the three laws of
recursion being satisfied in this piece of code? Let me know in the comments how
you think it follows the laws of recursion.
CONGRATULATIONS !!! You have learnt how to solve problems with recursive algorithm instead of an iterative algorithm. Keep practicing, try to find more cool problems to solve with this new acquired knowledge. Like I always say keep experimenting and learning new stuff ;)
Let me know, if you loved it, hated it, want to kill me or any other sort of feedback in the comments section below. Also, if you have any queries regarding the topics taught in this lesson or previous lessons, you can always find me in the comments section or in the telegram channel or on my pinterest profile where you can personally talk to me and ask me any question about anything we have learnt so far.
So, this was how you can use recursion
to solve your problems in Python. Stay tuned for another article next week,
same time, where we will discuss on a new programming topic. So more cool stuff coming your way,
DON’T MISS IT !! And I'll see you next week. Goodbye and Good Luck :)
I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like AND FOLLOW THIS BLOG TO BECOME A PROFESSIONAL PYTHON PROGRAMMER FROM A TOTAL BEGINNER. IF IT DIDN'T, feel free to ask any further queries in the comment section below.
If you are a beginner, intermediate, advanced or just someone interested in programming, feel free to join our telegram channel and be among people like you:
https://t.me/joinchat/AAAAAFVduJs3bF00nwKGUA
And do you know the best part? Joining it is FREE !!!
So go ahead click on the link and I will see you there.
You can also contact me through my email: code2learnofficial@gmail.com
or
HOPE YOU HAVE AN AWESOME DAY AHEAD !!!
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback