Thursday, January 30, 2014

Week Four - Recursions

Week Four - Recursions

This week we talked about recursions in class, in recursions, big problem can always be broken down into smaller version of the same problem. If a problem can be broken into the smaller version of the small probably, recursion is probably the best algorithm to use. Recursion always has a base case, which is the simplest form that problem can get. Recursions are extremely useful when working with nested list (like the sum and nested_depth function discussed in class). I found recursion very similar to math induction where you keep going back to the simpler version of the same problem. I found tracing a recursive function is relative easy as long as you start with the simplest case (i.e. the base case) and work your way towards more complicated cases. At the end, it is very likely that you can even predict what the recursive  function does without any more tracing. Take the class Tree_brust  we learned in lecture as an example, after level 2, we basically can predicate what level three will do without evening going through the whole tracing process again.

However, I am still having some difficulties in writing recursive functions myself, I can't think recursively and put  a function in recursive format. I guess I need to do some more practice on recursive in order to master the material. 

The lab this week is easier than the lab last week, it also involved some material in CSC108 (i.e. how to use a unittest class and how to choose good test cases). I also learned the SetUp and TearDown functions in unittest, I think it is brilliant that the unittest class has those two functions, where the SetUp method will set every thing up before the testing, and TearDown will clean everything up after the testing. So it would not create any problems in the test class below. This week, I managed to finish the lab early and had a chance to look at the property function and how to make a attributes "private". I am still kind of annoyed by the fact that python can't set an attributes as "private". Assignment one is also posted, I am planning to start working on it tomorrow.

Wednesday, January 22, 2014

Week Three - Object-Oriented Programming

Week Three - Object-Oriented Programming

Object-oriented programming is a very useful technique in programming, it creates object that has both data and function, and is often related to real-world situations. In the first week, we talked about how to make a data variable "private", and how users can access it using the "get" and "set" method. You cannot declare a variable as private in python, by convention all the "private variables" have an underscore before its name.

In week three, we first learned about inheritance. Every class we create is a subclass of class Object, and we automatically inherit all the methods and variable from Object class. And we can override those methods in the subclass, for example, the __eq__ method in Object class will return true if the two objects is referring to the same memory address. However, if we create a class we can write our own version of the __eq__ to decide how the two objects are going be equal. I find inheritance really useful, for one reason is that it can avoid duplicated code. For instance, if we are making a game, and we have Sword, Axe, Wand classes. If they are only different in some methods, it is more convenient to create a Weapon class and create all the common methods. Then create subclasses Sword, Axe and Wand, with the methods that are different.


I also found raising and catching exceptions a little frustrating, it took me quite a while to figure out what does a “try…except…” block do. From my understanding, if you raise an exception inside the try block, if there is any other code after this line, it will not be executed. But I have to say exceptions are also really useful, because it provides a way to deal with potential errors in the program. And if a program crashes, with an exception, it is easier for the user to figure out what the bug is.

Overall, I found this week's material easier to understand than last week (recursion), and lab #2 was also easier then lab #1.