Sunday, 2 February 2014

Week #4: Recursion and Inheritance

Recursion is mind blowing! What a good thing we have computers to calculate all this. Amazing! Just thinking about recursion sends my head spinning. What's recursion you ask? In computer programming, it's a code which repeats itself until the desired results are attained. Well, first of all how is it useful? Through it, you can calculate algorithms and formulas which have recursion in them, without the use of a paper and pen.

Recursion is a formula which repeats itself until a specific number. Once that number is reached, that number will be placed in the previous formula. That formula will be calculated and that number will be placed in the previous formula. That formula will be calculated and that number...well you get the point. This will keep happening until you get the final number. An infinite recursion is like when you put a mirror in front of a mirror...it keeps going until you can't see it. Have you ever tried making a camera face the screen that it's sending the image to? That becomes an infinite recursion and all you end up seeing is a white screen. In python, the recursion will run 1000 times and then send an error.

This is very useful in many circumstances. An example is for the Tower of Hanoi. This is a game which was developed where there are 3 pegs. On the first peg there is a certain number of discs which you need to transport from one side to the other. The main problem is that you may not place a bigger disc on a smaller one. The trick is to move all the discs but one to an intermediary peg (in this case the second one) and then put the bottom one on the desired destination. Then you repeat. So this is recursion. You move all the discs until the final one is left and then move the final one to the destination and then repeat. This carries on until all the discs are on the final peg.

This is the theme for our current assignment with one modification which makes a big difference. Instead of 3 pegs there are four. I understand how to do the moves but I can't think of a code which will make it work. We will be talking about it in class tomorrow so hopefully I work something out.

That's enough of recursion. Another thing which we practiced this week which we actually learned about last week is inheritance. I understood the concept as soon as the professor explained it but through this weeks' lab, I got more of an understanding of how to use it. Once again this is part of the OOP I talked about in my last post and it has (you guessed it!) many benefits. In the lab we also learned about setter and getter method. This is used so people don't mess around and change certain variables which you wouldn't like to be changed. This would be useful so that IF people try to modify the code, it doesn't allow them to exceed or even change a certain variable to a specific point.

Anyways, I know this was a bit longer compared to last week but I just had so much to talk about. Hope you enjoyed reading this. Until next time.

No comments:

Post a Comment