Friday, February 28, 2014

Recursion,[Re,[cu,r],[[s],ion]

This is the fifth week that I have known the recursion. I am getting more and more familiar with recursion. However, I am on the way to understand and to use it still.
 
First of all, I want to talk about my own opinion of recursion. The most straightforward way to explain recursion is that calling a function inside itself. 
 

This is a picture about recursion on wikipedia.
A visual form of recursion known as the Dorste effect. The woman in this image holds an object that contains a smaller image of her holding an identical object, which in turn contains a smaller image of herself holding an identical object, and so forth.

More precisely, recursion is a process of repeating objects in a self-similar way and stop repeating when it achieve its goal. 
 
In csc148, we often use recursion to solve the problem of nested lists because the process of dealing with the lists inside the big list is same as the process of dealing with the whole list.
 
Here is an example.
Def sum_list(L):
Lis=[]
For item in L:
If is instance(item, list):  # (*)
Lis.append(sum_list(item))   #where recursion happended
Else:
Lis.append(item)
Return sum(lis)
 
Lets create a nested list.
>>>[1,3,[4,2],[[4],7]]
 23
 
At (*), the function check whether the item is a list or not, if it is a list, this item will turn back and loop again until the elements inside  the item are all integers. This process makes a new list which contains all the integers in L without sublist or inner list inside it. Therefore, we are allowed to call sum to get the result.
 
 
Recursion really helps a lot when I was working on the assignment one. In Step.5 in assignment one, we were asked to solve four stool instances of Anna Hoy's cheese moving problem. When we try to move the cheeses, we need to determine if the cheese we plan to move is bigger than the top cheese on the destination stool., but if the destination stool is empty, we move the cheese without any judgments.  Hence the process for every move is the same. Using recursion here becomes a very good idea.
Here is my functions.
 
def tour_of_four_stools(model: TOAHModel, delay_btw_moves: float=0.5,
                        console_animate: bool=False):
    """Move a tower of cheeses from the first stool in model to the fourth.
       model - a TOAHModel with a tower of cheese on the first stool
                and three other empty stools
       console_animate - whether to animate the tour in the console
       delay_btw_moves - time delay between moves in seconds IF
                         console_animate == True
                         no effect if console_animate == False
    """
    helper = Helper(model, delay_btw_moves, console_animate)
    n = model.number_of_cheeses
   
    def move_cheeses_3stool(n: int, source: int, inter: int, dest: int):       
        if n == 1:
            model.move(source, dest)
            helper.pri()
        elif n > 1:
            move_cheeses_3stool(n - 1, source, dest, inter) #
            move_cheeses_3stool(1, source, inter, dest) #
            move_cheeses_3stool(n - 1, inter, source, dest)#
   
    def move_cheeses_4stool(n: int, source: int, int1: int, int2: int, dest: int):
        i = n // 2
        if n == 1:
            model.move(source, dest)
            helper.pri()
        elif n > 1:
            move_cheeses_4stool(n - i, source, int2, dest, int1)#
            move_cheeses_3stool(i, source, int2, dest)#
            helper.pri()
            move_cheeses_4stool(n - i, int1, source, int2, dest)#
           
    move_cheeses_4stool(n, 0, 1, 2, 3)
 
You can see that the line with # are where the recursion was being used.
Recursion is really interesting and useful. I like it and wish to know more about it. For programmers, it is really important to learn to be lazy which means that avoiding write repeating code. Recursion helps us achieve that.
 
Finally, using my picture as an example of recursion~~ lol~
 
 

Thursday, February 6, 2014

Talk about cs learning experience in midterm week.

This is a busy week. Although have to finish many tests and assignments this week, I paid my attention to computer science as usual. 
 
In this week, our lectures also focus on recursion. This weeks lecture really helps me understand recursion much better. The Tower of Hanoi is a very good example which shows how we can useful recursion is and how we can use recursion to solve problems in daily life. Another topic we discussed in our lecture this week is scope. Python looks or goes through the code in order. It is very important for us to know which order it is because this order can influence the efficiency of code. More importantly, it helps us know how our functions run which lets us know how to arrange the code properly and figure out the mistakes easily. By the way, we can clearly see the order which python looks with the online visualizer. 
 
 
Here, I want to talk about the assignment 1. I did not started it before yesterday because of my midterm tests. After I read the handout of assignment 1, I think that it is really interesting. When I was a child, I often play this game on my game boy! 
 
 

This game is harder than 星のカービィ (Kirby) which is more popular in our school. As a girl, I really like playing video games. I played games like Metal Slug with boys in our class and my little cousin when I was a primary school student. I think that is why I choose computer science. Computer is a really amazing invention. People can use it to do almost everything! I want to know it , to get familiar with it. All the things we can do are all build up on millions lines of code. This assignment reminds me of my first inspiration of computer. 
 
I will talk more about the assignment next week after I finish it.
 

 
 
 
 


Friday, January 31, 2014

Finding and fixing problems

In this week, working on computer science becomes more and more interesting. Our lectures, exercises and labs all improve my programming skills and let me understand well. 
 
First of all, I want to talk about my exercise2 which help me to practice using the Exception. In my opinion, ‘Exception’ is that we can except when something happens, the function will raise a error to help you figure out what kind of error your function makes. In the exercise2, I fixed a mistake for long time. I raise a ValueError when x cannot be convert to a integer with int(x) if x is a str. But actually we do not need to raise that because python can raise that error itself. Therefore, next time, I really need to read the handout carefully. And the next thing is the order of our exceptions. Before the exercise, I think the order of the if loop cannot influence the result. However, I fail a test in my e2b but my function has only one difference with others who pass the test. I did not find my error, but my partner helped me find that. He tested if the order can influence the result, and it worded. Therefore, I fixed this problem. I also think that let other people help you figure out your mistake is a good choice. Because sometimes people cannot see the error in their own works. 
 
Secondly, for my lab, it really goes on well. This is the first time I try to use unittest to test our function by ourselves. I and my partner at first thought our works are perfect. After testing it, we caught many error in it. Hence it is very important for us to learn to test by ourselves before someone else help you to find the mistakes. 
 
The most important thing I learned this week is that finding and fixing the problems. I think it really helps me in my computer science study.
 

Wednesday, January 22, 2014

Object-oriented programming

Our topic for the first journal is Object-oriented programming. After I saw this title, I asked myself that what is the Object-oriented programming? What I learned about Object-oriented programming in the lectures? What the Object-oriented programming do? 
I do not want to talk about the definition of Object-oriented programming in my slog. I just want to describe my opinion about it.
First of all, personally, I think class is something contains the characteristics of an object. For example, there is a class Dog. It maybe contains the gender or the color of hair of dogs. What information the class contains depends on what I want it to contains. 
For example, my class Dog is shown as follow:
Class Dog:
    Def __init___(self)
    Self.color = color
    Self.gender = gender
There also can be some methods in the class. If you want a dog who is a girl dog, you can create a method to see if it is girl. It will be seems like this:
Def gender(self, gender):
Return self.gender == female
In my opinion, we can store the information in our class and create methods to do everything we want it to do with the given information.