Week 7
Wowwwww! It's already the 7th week of this course, I am so surprised that the time in this semester went even faster than the last semester. Maybe I just have a lot to learn in csc148 and don't really have time to chill. I just done my first mid-term today and it was tough. I think I'm going to get probably a 50 out of 100. That means I probably have to work harder in the future.
So this week's topic is Recursion!!!!!!!! Some thing that is very useful but also can be extremely tricky if you don't get your logic right. Let me give a brief introduction to recursion.
First, start with the question, when do we need to use recursion? When we are solving a problem by repetitively using the same trick over and over again, that's the time we should use recursion. Recursion works like a while-loop, basically we put the header of the function in the return statement so when it gets to the return statement, it calls the function again. This is a very important logic in programming since it is used in many different programs. Here are some examples of recursions that are shown in class:
def rec_max(L):
"""
Return the maximum number in L, possibly a nested list
>>> rec_max([12, 25, 0, 3])
25
>>> rec_max([11, [18, 36], 0)
36
"""
return max([rec_max(x) if isinstance(x, list) else x for x in L])
As shown above, the function will go through everything in the list and when it finds a nested-list, it calls rec_max again and put the nested-list as a parameter, else, it'll append the number to the new list. Finally, return the maximum number in the new list. This is a very simple recursion and I believe more will be introduced in the coming weeks.
It is very important to find the stopping point of the recursion when we are writing it or it will go to infinite recursion and the computer won't have enough memory to handle it. Simple stopping points come from using for-loop over an iterable object, just like the example showed above. We can also create a condition so that when the recursion hits that condition, it'll stop. Just like a while-loop.
Recursion method is extremely useful since a lot of programs are repetitive and will be using recursion in it. For example, for the Binary tree that has been introduced to us, a lot of sub-functions in it are using recursion. In addition, Assignment 1 that we have handed in also require us to have a deep understanding in recursion in order to solve it. There is no doubt that recursion will be used very often when we are working as programmer in the future.
Honestly when the professor first introduced recursion, I was lost in the middle of the Pacific Ocean, have no idea what he's talking about and how the codes work. However, I found that when I actually grabbed a paper and pen and started the trace the program, it was ten times easier for me to understand the codes rather just read the codes over and over. Although it might take quite a lot of time to trace a recursive function, the process really gave me a lot of insight on how the recursive function works and when can I apply the recursive function.
Thank you for reading this blog. I hope I'll share with you soon.
No comments:
Post a Comment