2015年2月28日星期六

SLOG 5

Last week I talked about the concept of Object-Oriented Programming, so I may talk about the concept of ADT this week. I’m afraid I cannot explain it very well, since I still have a lot of questions on it.

What does ADT mean? It means Abstract Data Type, literally. But this name still cannot tell anything to a guy who does know computer science. The basic concept of ADT is a Stack that can be modified by adding or deleting the elements. Actually, we can only add or delete the first and last element of a Stack. 
This picture shows the basic meaning of Stack.

We can realize the modification by using the methods push and pop. For example, if there is a Stack like this: S = [1,2,3,4,5], we can push an element to the last of the list if we write S.append (6). Similarly, we can remove the 1 by the code S.pop (0).

2015年2月14日星期六

SLOG 4

As we have learnt Object-Oriented Programming  for several weeks, I think it is necessary to do some summary for this part.
To start an Object-Oriented Programming, the first step is to create a class( sometimes there may be more than one class) and give its meaning. Then we can start writing the definitions. Usually, the first definition we have to write is initials. It usually includes some key information that appears later.
  then we have to write some string method and representation method, which return a string version of self and a string representation of self.
Then sometimes we may need a method called –equal--, which is created to see if two pieces of stuff in the class are the same.
After writing down these normal things, we usually focus on the method that needs our real intelligence. Since we know this kind of methods follows what we want to realize, we cannot define them as we did above. We may understand them by using an example.
If we want to create something that can calculate the area of any triangle by knowing its three nodes, we have to firstly import the method Math, and then write something like this:
return (self.base**2 * 3**(1/2)) / 4

Above is how we write basic classes. And sometimes we need to write some subclasses for further usage. The subclasses inherit all the features of the basic class, and we can add the methods in the same way.

2015年2月7日星期六

SLOG 3

Since last week we had hand in our first assignment of this semester, we had a nice weekend that was not very busy, but we are quickly aware of a new task: the first midterm test is coming. We spent almost the whole Week doing reviews for the term test. We discussed together and figured out much knowledge that we didn't notice, or we can say, ignore the class. For example, we found that the concept of string and representation are almost the same, so we searched many websites and finally got the answer. Simply, string is written for a computer to read, and representation is used for a reader to comprehend. It was very important for us to totally understand them before the test since that can help us not mix their concept and write wrong codes.
 Another thing we met this Week is recursion. Recursion is a way of programming or coding a problem, in which a function calls itself one or more times in its body. So we can use it to express some method that may have to run infinite times, such as calculating the amount of the branches of a tree.
We learnt how to trace recursion. Suppose we have a function:

def nested_list(L):
    if isinstance(L,int)
        return L
    else:
        return max([nested_list(x) for x in L])

and we have a list: lst = [3,[2,1]], we can trace it:
Firstly, we observe the function and find out what it means. Here it means judge if L is an int,and return it if true. So call the function   [nested_list(x) for x in L] once, and we get 3 is an int and [2,1] not. The result is [3]. Call it twice, we have the result[3,2,1]. As the code says, we finally sum it, and the result 3+2+1=6.
Until now we can only read and find out the result of a recursion, but I believe that soon we will learn how to write a correct recursion on our own.