2016年4月5日星期二

Week 8

-Linked List-
This week I learnt about linked list. This is a common method of storying data. In linked list, there is a class called Node. The data is connected by node; the first node will linked to the second node. Linked list is a very useful method.

As objects/ nodes with a value, those objects/nodes also have a reference to the other similar objects. The basic structure is:

(12)()---->(99)()---->(37)()--->()



The structure of node class is:

class LLNode:

     """Node to be used in linked list
     nxt: LLNode -- next node
                               None iff we're at end of list
     value: object --data for current node
     """
   
     def __init__(self, value, nxt=None):
          """(LLNnde, object LLNode) ->NoneType

          Creat LLNode (self) with data value and successor nxt.
          """
          self.value, self.nxt = value, nxt

There are also front and back in code, which helped us to find the front and end of the list.

I learnt many helpful code from class, such as:

                                  cur_node  = self. front
                                  cur_node = cur_node.nxt

By using the code I can add or delete the end node. Besides the last node, former last node, back, size, possibly front could all changed by using the method.



I noticed from the lab that when writing code of linked list, it is easy for me to make a copy of the list first, since if any node changed the following data could not be find.

Week 6

Object-Oriented Programming concepts

"Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. "

The above is the definition of Object-Oriented Programming from wiki. In week 6, we finished the tippy using minimax method. The user could play tippy in python interface. In the game which I designed, the user could choose the range of graph that he or she wanted to play. Computer is the opponent, which will find the best option that could defense and lead to winning. 

The followings are the some structure of minimax:




It is very interesting, when writing the Tippy program, we need to find all situation that a tippy could put on the board:

  ++                ++           +               +
     ++          ++              ++          ++
                                         +         +
The computer need to use minimax method to choose the best place to put + on the board among all the options. And we also need to make sure the program can tell the user the result: win, lost or tie.

There are many details we need to think about, and we need to play the game for a while in order to make sure player could understand the game easily, and exclude any bugs.

Week 5

Recursion



We learned recursion. I think to understand recursion, knowing the basic structure of recursion coding is the most important thing.

The general form of recursion is:

         if <condition to detect a base case>:
         <do something without recursion>
         else: # <general case>
         <do something that involves recursive call(s)>

However, it is still not clearly show what recursion is by only understand the general form. In the class our prof provide us example about sorting. Sorting or counting numbers are some simple examples of recursion. 

The sorting of a list of number could be write as:

         def quick_sort(L):
               ‘ ‘ ‘(list) -> list
         if len(L) < 2:
                 return L
         else:
                 return (quick_sort([x for x in L if x < L[0]]) + [L[0]]          + quick_sort([x for x in L if x > L[0]]))

But we have to make sure L is a list or the type it should be in other exercise. 


Recursion is deeply related to binary tree. We can count the height, number of leaves or parity of the tree by using recursion.

2016年4月2日星期六

Slog 11

Slog 11 

I reviewed my Slogs from the first Slog to the latest Slog. I found out that I changed a little ideas about my slog. I think the I should pay more attention to the lecture than reading lecture by my own. I studies the Big Oh part by my own, but I did not get a clear understand about the concept of Big Oh. I read the lecture note but I cannot really understand what it means.
The below is a part from lecture: 


So I went to a different lecture to catch up the material. After the lecture I found out I understand what is Big Oh from the examples I gained from lecture. The merge sort:
def mergesort(lst):
    """ Return a sorted list with the same elements as <lst>.
    
    @type lst: list
    @rtype: lst
    """
    if len(lst) < 2:
        return lst
    else:
        mid = len(lst) // 2 # This is the midpoint of lst
        left_sorted = mergesort(lst[:mid])
        right_sorted = mergesort(lst[mid:])
        # "merge" the two sorted halves (how?)
        return merge(left_sorted, right_sorted)

For merge sort it starts with a list of length of n and divide the length by 2 until it reach a list of length 1, the recursion depth of merge sort is the number of times you need to divide n by 2 to get 1. The number k such that 2^k ≈ n. Using logarithms we get the Big Oh is O(nlogn).


If I did go to lecture every time and listen carefully to the prof I should get less questions in the earlier slog. I think I would be confused about binary search tree or linked list any more.

2016年3月3日星期四

Week 7


SLOG Week 7

In last week’s lecture, I learned about recursion, which I think is a very interesting and useful topic. In the class, the professor gave us several practice of recursion. I think it is hard. It is hard to figure out the base case and hard to follow the recursive steps. I find writing down each step of recursion is a good way to understand a recursive function.

Also the recursive examples on the website helped me to understand the recursive concept a lot. I wrote down every step for each example, such as nested_counts and nested_contains. Sometimes I did not write the correct step, than the result was incorrect. I  think I am improving after doing the exercise.

To write the recursion code, the most important thing is writing a good base case. The base case is what the recursive steps calling at each time of recursion. And the code always follows this format:


             def f(obj):
                 if isinstance(obj, int):
          ...
    else:
         for lst_i in obj:
            ... f(lst_i) ...


The base case should appeared in the if part, and the else part is the recursive calls. If the recursive function is not correct, then the base case and recursive calls might be incorrect. I think I am going to use recursion many times in assignment 2. 


2016年2月22日星期一

Week 4 Slog

WeeK 4

This week I learned about stake and queue. I think it is interested. 
I find the following summary about abstract data type online, and find it is really useful:
An Abstract Data Type is a particular kind of class that's defined purely by its interface rather than its implementation - that's what makes itabstract. You're already familiar with three abstract data types:
  • lists: store elements sequentially. Access elements by index, add new elements to the list, determine if the list is empty.
  • dictionaries: store key-value pairs. Look up a value based on key, add/remove key-value pairs.
  • files: objects that you can open for reading in data or for writing data. Start reading from the beginning of a file, starting writing at the beginning of a file, skip to some part of a file.
As you saw in CSC108, you can do many, many useful things with lists and dictionaries - but this power is a double-edged sword. The more operations an interface offers, the harder it is to implement, and to use! This week, we're going to look at a much simpler ADT: the Stack.


stack follows LIFO rule, which is last in first out. Stack is also the place to store the memory. I did the practice of stack and queue. I think the remove part is the hardest part to understand. In the assignment 1, I write about the priority queue. In priority queue the highest item will be remove first. 

2016年1月28日星期四

week3

Slog of Week 3



During the first three weeks’ lecture, I learned about class. I was amazed when I saw the perfect square and the perfect triangle showed on the screen. It is the first time I thought about python could do a lot of things way more interesting than just get a list of students name or make dictionary. 

However, the class material was confused at first. I could not follow the lecture in the first hour. I kept reading the code of ‘Shape’ again and again, and tried some code by myself. Then I felt a little confident. From the last week’s lecture I couldn’t figure out what a subclass is. But in this lecture I understand how to make a subclass. 

In the class I learned a lot of materials, which not only about coding, but also the ideas. I love the discussion part on the lecture. Our professor let us talk about ‘is a’ and ‘has a’ relationship. After the discussion, I think I get a much clearer understanding on ‘class’, ‘object’, and ‘instance’.

A object could be a class, but class is not an object. For example, a chair is an object. When people ask me to bring a chair, then the ‘chair’ is a object. The class attribute of ‘chair’ could be the concept definition of color, but the object attribute could be color of the chair.

Although in the lecture, our professor guide us to write the class of ‘Shape’. But I will rewrite the ‘Shape’ python code so that I could learn the material better. And I am looking forward to learn more about subclasses and inheritance in next week’s lecture. I hope I could do a good job in this course!