Ahhhh Recursion, the most interesting thing i've come across in 148 thus far. Looking at it for the first time was clearly A LOT of confusion. Thoughts of "Why is it calling itself in it's own body, what does it even do, can it even do that?" and being confused for about a week until I woke up one morning, and i'm not kidding you when I say I had an epiphany after I dreamt about trying to solve a recursion problem in my sleep. Haha I'm laughing as I type this because this seriously sounds insane, even to me. No joke, I can kind of explain this phenomena by something I learned while studying for my Psychology midterm on Tuesday. It's called Problem-solving theory where someone solves the problems that they encounter when they're awake in their sleep and dreams.
Anyway, that's a fun story for a SLOG but anyway recursion is a concept where it involves breaking down bigger problems to smaller problems and smaller problems until it can be solved trivially. After thinking about it for a while on my train ride to my 11AM 148 lab after my 'dream', I was envisioning how Python reads this code and what i've come up with is that it checks the input that you give it and sees if its the type of object that "isinstance" wants it to be (well in the case of the recursion problems that we've worked on in the lab). If it is not the specified object type that "isinstance" wants and is checking for, it goes to the else statement and returns whatever it wants it states to return. But if it is, it goes to a return statement that involves a list comprehension where it loops over that list and calls the method again until each nested list is computed and until it can be solved trivially which is pretty neat. Well this is my understanding of how to trace the code.
Tracing a bunch during the lab was really really helpful in understanding how Python goes through a nested list using recursion but practicing it over and over and tracing it, you really get the hang of it. I'm sure I need more practice in understanding how it loops over as I probably messed up my test because I got confused at one point but hey, i'll learn from my mistake. I also need to practice more because i'm sure we're going to have to write a bunch of them soon and it's a bittersweet feeling. Bitter because i'm sure it'll be tough in the beginning but sweet because who doesn't love problem solving questions and it's very useful especially for calculating something in nested lists and other things! Yay, more programming fun.
P.S. Recursion is pretty difficult to understand but when I was stuck, I stumbled upon this website (here) and really helped me understand the concept a little more. Hope this helps you as well! Cheers xoxo.
The link you provided is really helpful. Nice post!!
ReplyDelete