Sunday, 23 February 2014

Week #6: Trees

This week we learned about a different type of stacks. Theses were called trees. They were made into a separate class of their own and we were taught the different terminology of the Tree class. Each value was called a node. Each node could only have one parent meaning that it was only part of one Tree class and not multiple. The node at the top was known as the root and there was only one unique path from the root to each node. There were no loops.


Within the nodes there were terminologies to make nodes specific. They are as follows:

  1. Leaf - A node with no children.
  2. Internal Node - A node with one or more children. (It's possible for a node to have more than one children but not more than one parent.)
  3. Subtree - A tree formed by any tree node together with its descendants and the edges leading to them.
There were also two other terms:
  1. Height - The maximum path length in a tree. Each node is counted as a height of one. So, the maximum number of nodes from the root to the final node would be the height.
  2. Arity, Branching Factor - The maximum number of children for any node.
I have absolutely no idea how this is going to help us as of yet but I've taken a look at assignment 2 and it's based on designing a class which is similar to this. We'll see how that goes.

Also last week in the labs, we had to do some work which made us think outside the box. We had to use loop to recreate some built in python functions. The lab was pretty straightforward and not too challenging. 

We've got the midterm this week and just received an email saying that we're allowed to bring one cheat sheet with stuff written on both sides. I'm not quite sure what to write on it but I've got two days to figure it out. Anyways I hoped you enjoyed reading this and that the explanation of the Tree class was clear (to be honest, I actually was pretty lost until I wrote this. Now I've got some sort of clue to what's going on.) Until next time.

Sunday, 9 February 2014

Week #5: Scopes and Namespaces

This week we started with the Towers of Hanoi recursion algorithm. The one which was shown in class was the one used for the 3-stool problem. I had already figured out how to write that one and had implemented it. The result I got was not the optimal solution but thought it could work as I couldn't get the recursion function for the optimal i to work. Luckily after much research, I found a different way to find the i. After that, I worked on a solution to solve the 4-stool algorithm. So I finally completed the assignment with the optimal solution. All I've got left to do is to just go through my code and clean up the docstring and the code by making sure that all the names are coincided and not repeated.

The other things the professor talked about was quite simple. They were things which I had come across while I was writing code in CSC108 and came across many errors. These errors helped me understand how the naming worked.I didn't know there would be a whole lecture dedicated to how it worked. When the professor was showing us the example, I got it straightaway due to experience. For more information on namespaces and scopes check http://docs.python.org/3.3/tutorial/classes.html#python-scopes-and-namespaces.

This week in lab we did some more practice on recursion. We just traced the recursion on paper and then wrote a bit of code which was a bit challenging. But my partner and I got through it with a bit of help from our TA. Other than that this week was a pretty slow week. Not too interesting. Hoped you enjoyed reading this. Until next time.

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.