2015年4月6日星期一

Final Slog

 The semester has end and this is the last slog of my CSC148. 
 Now, looking back over this course, I 
really learned a lot. Now I understand that CSC108 is just a course that taught us how to code in
python. CSC148 is the course that lead us into the field of computer science. We learned how to 
create a game by assignment1. Although we were 
only required to implement random strategy in 
assignment1, I still feel it very difficult 
because it was the first time that I implement a 
whole game on my own, well, not on my own exactly, with my team mates, but it was their first time 
too. We don't know what classes to create. But we figured it out with the help form Piazza. After 
assignment1, I figured out how to implement a game and the basic concept of object-oriented 
programming. Then it came assignment2. This time the “class” thing was not the most challenging part, the strategy was. We stuck on strategy minimax 
fora long time. After figuring it out, our skill 
of implementing recursive functions really 
improved a lot. Assignment3 was more like a lab 
exercise.
 In lectures, we learned object-oriented 
programming, recursion, tree, linked list. Last 
few lectures we talked about the efficiency of
functions. The same topic was also mentioned in CSC165. 

 In conclusion, CSC148 is a very exciting 
and challenging course.  

A Look Back

 I revisit all my slogs, and I found that I agree with myself in every slog. But after reading some other people's slogs, I realized that although I didn't say something that I don't agree with now, but my point of recursion was very incomplete and useless.
 In my slog Summary of Recursion, I just 
simply introduced what recursion is and gave some 
example in the text. Yesterday I saw my friend 
Yuli's slog. She said that she didn't agree with 
herself because she used to think recursion is 
interesting and easy! I think the most important 
thing she wrote about in this week's slog is the 
method to implement a recursion function that she 
concluded herself: “The key part of recursion is 
to figure out the base case and the body. So, just like the instructor says, every time you write a recursive code,work from the base case up, and then test your base case. Once you are sure it works, test a problem one step larger.” I think this is a very useful conclusion. My friend Wenzheng also
choose to wrote about recursion in this week's slog. He gave two tips on implementing recursive 
functions: “Understanding the algorithm before 
writing the function.” and “Drawing a picture.” 
And he said that drawing a picture is the most 
important step because it helps you understand how the function work.
 The tips from Yuli and Wenzheng are really useful, I will use these tips to master recursion.

 

2015年3月22日星期日

Week 10

Last week we had a term test. I made a mistake in a question about preorder. Preorder, inorder and postorder was in lab 06. But it’s the last three functions in lab 06, so I guess I didn't pay much attention to it when I go through the lab 06 sample solution. And also, I implement the required functions based on my cheat sheet. In term test 1, I forgot to make a cheat sheet so I implement all functions on my own during the test and got a good mark, which made me really happy and confident. But now I don’t know if I can implement them all by myself without the help from cheat sheet. And I know we should still go through the lab handout and implement all those functions by ourselves even though TAs are on strike, but I was lazy and didn't treat the lab material seriously since we didn't need to go to lab.
We only had one lecture last week. We talked about some binary search tree insert and delete functions. What I learned from the lecture is if you let a node refer to nothing or nothing refers to it, python will delete it automatically.
Unfortunately, TAs are still on strike so we didn't have lab last week.

The weather is warm. I am so happy the winter is over.  

Week 9

Last week we talked about linked list in lecture.

Linked list is another kind of data structure. It consists a group of nodes, with every node be the reference of next node. We defined two classes: LLNode and LinkedList. We use LLNode to represent each node and LinkedList to represent the whole linked list. Each node has a value and a “.nxt”, and each linked list has a back, a front, and a size.
(linked list)

Because the TAs are still on strike, we didn’t have lab this week. But I read the lab handout and the sample solution. This lab was mainly about the basic property of linked list. We were asked to implement functions like delete a node, or find a node. We haven’t use recursion in linked list yet.


I noticed it’s really easy to forget to change the size of a linked list after you delete or insert a node in it. I should pay attention to it in the test. And the test is coming, I should start to do some past papers.

2015年3月21日星期六

Week 8


In week 7 we learned about another abstract data type, binary tree. Binary tree is a kind of tree with special property.
(binary tree)


(binary search tree)
Each node in a binary tree has at most two children, left child and right child. There is a special kind of binary tree called binary search tree. The value of its left child is smaller than the value of the current node, and the value of its right child is bigger than the value of the current node. Because of this property, we can search for data efficiently in a binary search tree. For example, if you want to know if a binary search tree contains a value:
def bst_contains_v1(node, v):
    ''' (BTNode, v) -> bool
   
    Return whether the binary tree rooted at node contains v.

    Assumption: node is the root of a binary search tree.
     '''
    if v == None:
        return False
    elif v == node.data:
        return True
    elif v < node.data:
        return contains(node.left, v)
    else:
        return contains(node.right, v)   
If you want to search for a value in a binary search tree, you don’t need to search through the entire tree. You can compare the value with the value of current node. If the value is bigger than the value of current node, you don’t need to compare it with the nodes that are rooted at its left children.

We had an assignment due the following week, my teammates and I all worked very hard. Using recursion in our assignment is really interesting and challenging.

2015年3月8日星期日

Summary of Recursion

This week we talked about binary tree in class. The recursion we saw before were all in the style of:
  if: ……
  else: ……, but in the code of bst_contains_vl(node, v), we saw recursion in:   if: ……
  elif: ……
  elif: ……
  else: ……
Recursion means a function calls itself to solve problems. It’s really convenient to use recursion in some situation. For example, count the element of a list. People might say ”Oh, this is easy, we can choose a counter n, then for every element in the list n = n + 1. We can do it without recursion. ” This is doable when the list is very simple like this: [1, 2, 3, 4, 5]. But what if the list look like this: [1,[2,[3,[4, 5]]]]? Then it’s very difficult to solve this problem in a traditional way. This is why we use recursion. If we use recursion, the function itself will look for the numbers in the list:
  if not isinstance(L, list):
      return 1
  else:
      return sum([count_element(c) for c in L])
You may think you get the idea of recursion after some practice, but you will know you are wrong when you see this:
  def M(n):
    if n > 100:
        return n – 10
    else:
        return f(f(n + 11))
It’s called McCrathy 91 function. The results of evaluating the function are given by M(n) = 91 for all integer arguments n ≤ 100, and M(n) = n − 10 for n > 100. Indeed, the result of M(101) is also 91 (101 - 10 = 91). All results of M(n) after n = 101 are continually increasing by 1, e.g. M(102) = 92, M(103) = 93. I try to trace this function, but the process is really hard.

Recursion can be very challenging, the only way to understand a recursive function is to trace it.

2015年2月17日星期二

Week 6

 This week, we get our feedback of Assignment 1 and Term Test 1.

  Before we learned recursion, we focused on object – oriented programming, and assignment 1 was all about it. To be honest, I am a little bit proud of myself. We learned how to develop a class in CSC108, but we didn't have a chance to write our own classes. After finishing assignment 1, I get the whole idea of object – oriented programming. When programming, you must have a whole picture of what you want. Take Assignment 1 for example, you want a program from which you can choose many different games to play. First, you need generic class for both GameState and GameStrategy and a GameView class for people to play with. Then, develop subclasses piece by piece according to those generic classes until you meet all requirements. I think the key concept is: don’t start form details but start from structure.


  Still, I encountered a lot of problems when doing Assignment 1. I didn't know what to put in the parent classes. Details were undecided so I couldn't write any code including details. Everything must be generic. And I thought the parent classes are useless because they were so generic and simple that I still need to write many specific code in the subclasses. But then I understood, writing parent classes would make it easier to write subclasses because you don’t need to repeat code you already wrote if the subclass requires a method which does the same thing that the method in parent class does. And you will know what you need to implement clearly because you already built the structure when you wrote the parent classes.