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.