Sunday, February 23, 2014

List comprehensions, zip and filter.

In week 5 lab, we learned something new called list comprehensions, which can reduce your code whenever you have to loop over your list. Normally we use a for loop and an if statement within that for loop to modify the list. By using list comprehensions, we can easily use one simple line to do the job. For example, if we want to generate a new list of odd integers from 1 to 10, using for loops. First we need to create an empty list, then for each number in range from 1 to 10, we append only the odd number among all to the new list. By using list comprehensions, we can just write new_list = [ n for n in range(10) if n % 2]. Nevertheless, when the circumstance becomes too complicated, we prefer to use the for loops.
Zip is a bulit-in class in python, which helps a lot when we use list comprehensions. By defining a new class of  zip on two lists, it will return a list of tuples that containing each item in both lists. Of course one that uses zip() has to note that lists with different length have to be treated differently.
Filter is a bulit-in function in python that can be use in list comprehensions as well. When we generating a new list from the old one, we often set certain conditions on the item we want. The if statement usually does the job in a for loop. Here by using filter(function, iterable), it is equivalent to the generator expression [item for item in iterable if function(item)].

Hanoi Tower

In the first assignment of the term, we were introduced to a game called Hanoi Tower. This mathematical puzzle consists of three rods and different sizes of disks that can be moved onto any rod. Some rules apply to the game, that bigger disks can not be stack onto smaller ones. To win the game, someone has to move all the disks from the original rod to a destination rod within the least moves. The tricky part was that we had to deal with the more complex version.
Four rods were given, and we were required to find the most efficient solution on this task. Me and my partners followed the steps provided. None of us foresaw the difficulty of this assignment until step four which we had to write recursions for the solution. We suddenly realized that within four rods, the whole game changed.
Instead of three rods, using four rods gives us one more intermediate rod which makes the case more complicated. The strategy of playing this game is to choose a number between one and the number of total disks (call it n) in the game, call it i. We first move i disks onto either one the intermediate rods by using all four rods here. Then we move n-i disks onto the final destination rods using three rods that are available. Last, we move i disks onto destination rods using four rods. After a few rounds of moving, we realized that we could play the game by using recursion. Recall that recursion is a method that to run a function which contains this function itself. Here in this case, we have to use three recursion one after another. First, we use a recursive function of moving disks within four rods, then a recursive function of moving disks within three rods, and lastly another recursive function of moving disks within four rods.
The key to find the least moves is the choice of i. We discovered that within 10 disks, we chose i to be the middle number of the total n. However, when the disks increase to more than 10, we failed to find the most efficient way of solving the problem.

Sunday, February 2, 2014

Inheritance

Can you believe that there are only 8 weeks until the semester ends? In the last week's lectures, we have been introduced a concept called 'Inheritance'. Inheritance is used to indicate that one class will get most or all of its features from a parent class. Another way to do the exact same thing is to define another class and modules. There are three ways that parent class and child class interacts: (1). Actions on the child imply an action on the parent; (2). Actions on the child override the action on the parent; (3). Actions on the child alter the action on the patent. In the lab of last week, we mainly practiced on how to apply the first two interactions. Also, we wrote some test code by using the unit test mode. During the lectures, a new concept called "Turtle" was introduced. I have not fully understand this whole new thing yet, and definite need to work on it more. After all, happy Chinese New Year to everyone, and wish you all the best!