Hello Y'all,
Today's the first day back from reading week. I'm actually happy that school started again! Now, a quadruplet:
In the scalding waters of Uoft was he baptized
So far said he, Reading week? t'was a good revive
Before the Mid-term week demise
He prayed e'rday that he'd not be eulogized...
In a way I am a sadist, subjecting poor, over-worked and down trodden TAs to my god awful poetry. But hey, I have an audience! And they have to read it! Mwahahahaha...
Anyways, back to recursion.
As you would have noticed from my post last week, recursion completely escaped my mental grasp. Whilst I generally agreed with the concept, I didn't understand it well at all. In particular I didn't understand what was happening. Then in class I thought I heard Danny ordain, "Understand it by the stack method!". So I went home and tried this...It was an utter disaster. So I wrote to the rightful minister of the Church of Python and Father Heap said, "Son, you have sinned! I said DO NOT UNDERSTAND it by the stack method. Learn it the human way, plug in unknown values!"
So I did this.
Now, a full week after receiving the Holy Father of the Church of Python's advice, I understand recursion.
You cannot imagine what a relief this is, along with no small measure of satisfaction.
So, what is recursion in my words? A few points that I have understood
1) Don't worry about details when understanding what the function does
2) First understand the big picture and then move to figuring out the details
3) Recursion must have: a base case, a recursive call and the recursive call must be on smaller input.
How to code a recursive function?
1) Think about what will happen in the base case. ALWAYS start with the base case.
2) Once you have that down, then work on where the recursive call will go in your function
3) Make sure that the input to the recursive call is smaller than the input you started with
It's really hard to put in words my understanding of recursion. I agreed with the principle before, it's just that now the 'penny dropped'. It just makes sense now.
Debugging?
1) If you have a max rec depth error, either your recursive call is not working on a smaller input or your base case is not properly defined
2) Any other errors, such as wrong output values etc, just trace through your code starting from the smallest possible unput and work your way upward and see what happens.
I personally find it extremely helpful to trace through the recursive code on a input that's just one bigger than the case to visualize what is happening.
Is recursion useful?
Definitely, why? Because it is an intelligent way of coding. As the Holy Father has said, 'laziness my flock, it is the first virtue'. Since we wish to do the same thing over and over and over again, why rewrite the code again and again? This is where recursion comes in.
I think that many problems that could be solved by recursion could also be solved iteratively, but just thinking about the mess of that code is giving me a headache. Bottom line - recursion saves work. It may take a little longer to get started with the code because it requires more thinking but in the long run, it's a lot less overall work.
When to apply?
For me - If I hear myself say the words 'do this and then do this again' or 'over and over again' that means think recursion.
It's really easy if you sound off the way you're going to solve a problem.
For example -
I want to calculate the number of peaches on a peach tree. Suppose that the leaves can be either well leaves or peaches.
So I would say to myself, well I will go down the first branch and see if the first thing is a leaf or peach, then do that again for the second, then the third etc etc
I hear myself saying, 'do that again', which tells me to do the same thing which means recursion.
An example?
Here's my example of useful recursive code:
1) From a family tree, figuring out how many boys were born versus the number of girls.
2) Drawing beautiful fractals. I took MAT335 and the pictures in that course are really just breath-taking.
3) Recursively drawn video-game graphics. This is a fun one. Suppose you are in a forest. Then rather than making an entire forest, you just make portion of it. Then you use that to recursively make the entire forest whilst adjusting certain values such as the number of palm trees, number of rocks, number of birds etc etc
And finally, the Pièce de résistance :
Even GOD (the real one, not of Church of Python) uses recursion! Broccoli, Sunflowers, that fossilized sea animal, shells! How can it not be useful! (By the way, does this imply that one of god's virtues is laziness?)
Sorry for the long post.
Kuda Hafiz,
Seigal
Monday, 24 February 2014
Monday, 17 February 2014
Re-can't understand-sion
Hello Folks,
This last week has been an absolute nightmare.
A1 was due to Thursday, then I had another huge problem set due Friday, and another due Saturday AND even another due Sunday...AND even another due Monday.
Anyways, I guess this is part of school life. But back to CSC148, I am really struggling to understand how recursion works.
I understand the concept behind it that, that we just call the function that we are defining on a smaller input.
But the "penny" hasn't dropped on how this works. How does the function know what to do with the smaller input? How do we stop it from going on for ever? It's all very confusing.
Anyways, that's what I'm working on over reading week.
Bye,
Seigal
This last week has been an absolute nightmare.
A1 was due to Thursday, then I had another huge problem set due Friday, and another due Saturday AND even another due Sunday...AND even another due Monday.
Anyways, I guess this is part of school life. But back to CSC148, I am really struggling to understand how recursion works.
I understand the concept behind it that, that we just call the function that we are defining on a smaller input.
But the "penny" hasn't dropped on how this works. How does the function know what to do with the smaller input? How do we stop it from going on for ever? It's all very confusing.
Anyways, that's what I'm working on over reading week.
Bye,
Seigal
Wednesday, 5 February 2014
Dazed & Confused (& Panicked) :(
Folks,
The real flavour of CSC148 is coming on strong now.
This week we covered scopes for names in Python. It seems relatively straight-forward so I have nothing to say here.
However, last week's lab on unittest just blew my mind. Did you know that when using the unittest class you need to write test in front of each test case. I pulled my hair out until the very end of the tutorial when the TA mentioned that this was needed. Why Python? Why? The rest of you allows for such flexibility with names and here you want this?
This calls for a sonnet.
There once was a boy
He thought he knew Python
But unittest showed he knew bok-choy
And his confidence - a bygone!
To Danny : Please teach us unittest again
Thanks for reading the bad poetry.
Oh by the way, the panicked part in the title - Not started A1(!) FML.
Cheers,
Seigal
The real flavour of CSC148 is coming on strong now.
This week we covered scopes for names in Python. It seems relatively straight-forward so I have nothing to say here.
However, last week's lab on unittest just blew my mind. Did you know that when using the unittest class you need to write test in front of each test case. I pulled my hair out until the very end of the tutorial when the TA mentioned that this was needed. Why Python? Why? The rest of you allows for such flexibility with names and here you want this?
This calls for a sonnet.
There once was a boy
He thought he knew Python
But unittest showed he knew bok-choy
And his confidence - a bygone!
To Danny : Please teach us unittest again
Thanks for reading the bad poetry.
Oh by the way, the panicked part in the title - Not started A1(!) FML.
Cheers,
Seigal
Subscribe to:
Posts (Atom)