So turns out that I have successfully survived the first 4 week of classes. Did I just say 4 weeks?? wow that sure was fast.Anyhow, this fourth week was quite interesting I must say because it wasn't until the beginning of this week when I realized that the next week I was to be writing the midterm for CSC148. With the upcoming test preparation and the amount of stuff due in my other classes including Assignment 1 for this course, week 4 was a hell of a ride.
Now, lets talk about the interesting stuff. Or I should say, the new material we studied during week 4. We were introduced to Recursion. Recursion is a way of coding a problem in which a function calls itself multiple times in its body. For now we are asked not to worry about writing the codes for recursion and focus on tracing them. To be honest, this was the first time that after the lecture it felt like I actually knew what was going on during the lecture. I was actually able to follow along with the prof. I want to say its an easy concept but I dont also. The way Prof. Danny went over the material explain step by step how recursion works and how to trace it, made it easier for me to understand the material.
The code for Recursion is as followed:
if isinstance(L, list):
return sum([sum_list(x) for x in L])
else:
return L
Now lets look at how a simple list will be traced:
sum_list([4, 1, 8]) --> sum( [ sum_list(4), sum_list(1), sum_list(8) ])
# here we go over the code presented above to see whether sum_list(4) is a list or not if it is not we return the value of L and if it is then we sum the values of the list.
--> sum([4, 1, 8])--> 4 + 1 + 8
--> 13
A nested list would look difficult to trace, however it is quiet easy to trace:
sum_list([[1, 2, 3], [4, 5], 8]) --> sum([sum_list([1, 2, 3]), sum_list([4, 5]), sum_list(8)])
#here we add the integers of each list separately:
--> sum([6 (1+2+3), 9 (4+5), 8])
#now we have a list of 3 integers that can be added by using the built-in function sum:
---> 23 (6+9+8)
I know it might be too soon to say this, but Recursion has to be the easiest material to grasp on for me. For me it was a very basic concept and something that I can actually use later on for coding. Also, since I actually understood the material, I actually enjoyed the lab of this week because I was able to finish the lab and do well on the quiz. Out of all the topics we learnt in class, my favourite topic would be tracing Recursion. Like I said in my previous post, "Practice makes a man perfect", this truly holds as the more I practice using the additional questions on the lab handout, I better understood Recursion as well as other topics.
This is what I actually got out of Recursion, I would love to hear what you think and please feel free to add to my understanding of Recursion if there is anything major I missed on via commenting on this post. Thank You for reading my not-so-funny posts. I will leave you all with this pretty picture presented to us in class that used recursion: 'The Recursive turtle example":

No comments:
Post a Comment