Sunday, 29 March 2015

Week 12: Last Impression

That is my last slog for CSC148, I still can't believe we almost at the end of the semester. I still remember the first slog required to write is why geeks should write. Now, I can conclude this question deeply with the experiences of writing slogs whole semester. Compared without slogs, students can recall all the new knowledge per week at the end of the week, which can improve the way of studying because students can find the critical points of every methods of classes. For example, I found that the sample solutions are much easier and quicker to understand and code when I learning the LLNode. To be honest, I cannot know the critical point without slog, since I found this point when I read others' slogs.
On the other hand, we have learned lot of about class Tree, which is used in to store information one by one layer. First one is the class Tree with arbitrary nodes per layer, second one is class BTNode with 2 nodes per layer and it has smaller number on the left, third one is class LLNode with one single node per layer and it can used in linked-list which has a LLNode on the front and back respectively.
However, we all feel the recursion functions are most challenging new knowledge that we learned this semester. I suggest that understand every sample solution step by step, which is doing the tracking.
Hope we all have good marks in CSC148!

Sunday, 22 March 2015

Week 11: Impression of earlier blogs

I have reviewed my earlier blogs, most things are correct unless one thing. I said Stark and Queue will be very useful in the future; actually, it is true, but I expected it will be a part in our test which is certainly did not happen. Stack and Queue are more like the helper functions for class Tree.

I have read some blogs from my classmates, one of them is really impressive. He or she wrote the details of tests, I have never wrote anything about tests. Besides, he or she also wrote the very deep analysis of every class and methods. I really appreciate the way of he or she writing, I think I pay too much attention on how I learn he new knowledges but not so much on the knowledges itself.

It almost end of the term, I think the most important things I learned are: recursion and Tree. Actually, besides the csc148, I also take another course needing python is phy324. I had used recursive functions to code the computational assignment. Recursion can solve the problem shortly and simply. For tree, I guess it would be useful in store some data and information orderly.

Next mission is A3, good luck for us!

Sunday, 15 March 2015

Week 10: My impressions of week 9.

This week, we had a test, I had reviewed a lot of stuff.

The most important thing I reviewed is the sample solution of LLNode. I had confused about the sample solution for a time, I did not understand the relation between the current_node and lnk.front. I asked myself: when the current_node move to the next node, why does not the lnk.front move to next one? I thought the lnk.front also assign to its next node if the current_node did.

However, the relation between them is not simple like that after I read the slog of a classmate, his or her blog is: zhexinlai.blogspot.ca. Actually, they works like when the current_node moved to the next node, it seems like walking along the lnk.front so that can get the deep nodes of lnk.front, which completely does not change the value of lnk.front at this step.

Besides of that, we learned the two important functions working on BTNode: insert and delete. The writing idea also is recursion, but it is not hard to understand. First, the function insert ensure that the left side value is less than the right one, so the idea of delete is to using the property of that left side node.value is always less than the right side one that we can avoiding the unnecessary step in the body of the function.

I really hope school can stop the strike, it deeply influenced our study plan because of no lab.

Monday, 9 March 2015

Week 9: The Impressions of Week 8

This week, we learned a brand new thing -- LinkedList and LLNode. 

Personal speaking, I feel LLNode is easy to understand, since it is kind of a tree with single child. However, for LinkedList, I was little confuse because of the structures and usabilities. First, why does  not the lab have lot of functions about lnk.front but not so much about lnk.back? Secondly, I do not know where we can use LinkedList in coding programs. 

Except as these two questions, my solutions of functions are totally different compared with the sample solutions, although it can run very well. I guess there are multiple ways to write a same function in computer science. The main concern is all about running time. For example, one of my my function needs 10 seconds to run a result, but the corresponding sample solution just needs 1 second. I should learn how to write function like sample solutions and understand the better ideas of coding. 

Let me take self.__setitem__ for example to show the different of sample solutions and mine: 
sample solution:
    def __setitem__(self, index, value):
        ''' (LinkedList, int|slice, object) -> NoneType

        Set the value at index to value, if index is in range, otherwise
        raise an IndexError.  Indexs are counted from 0. Note that negative
        integers can be adjusted by adding self.size, to get a index in
        range.

        >>> lnk = LinkedList()
        >>> lnk.prepend(5)
        >>> lnk.prepend(7)
        >>> lnk.__setitem__(1, 9)
        >>> print(lnk.front)
        7 -> 9 ->|
        >>> lnk[0] = 3
        >>> print(lnk.front)
        3 -> 9 ->|
        >>> lnk[-1] = 8
        >>> print(lnk.front)
        3 -> 8 ->|
        '''
        if index >= self.size or self.size == 0:
            raise IndexError('Out of range')
        elif index < 0:
            return self.__setitem__(self.size + index, value)
        else:
            cur_node = self.front
            assert 0 <= index < self.size, ('Index {} too big for node {}'.
                                            format(cur_node))
        for step in range(0, index):
            cur_node = cur_node.nxt
        cur_node.value = value
Mine:
    def __setitem__(self, index, value):
        ''' (LinkedList, int|slice, object) -> NoneType

        Set the value at index to value, if index is in range, otherwise
        raise an IndexError.  Indexs are counted from 0. Note that negative
        integers can be adjusted by adding self.size, to get a index in
        range.

        >>> lnk = LinkedList()
        >>> lnk.prepend(5)
        >>> lnk.prepend(7)
        >>> lnk.__setitem__(1, 9)
        >>> print(lnk.front)
        7 -> 9 ->|
        >>> lnk[0] = 3
        >>> print(lnk.front)
        3 -> 9 ->|
        >>> lnk[-1] = 8
        >>> print(lnk.front)
        3 -> 8 ->|
        '''
        length = self.size
        l = []
        for i in range(0, length):
            l.append(self.__getitem__(i))
        for i in range(0,length):
            self.delete_front()
        
        if index < 0 :
            index = index + length
         
        if index > length - 1:
            raise Exception('out of range')
        else:
            for i in range(0, length):
                if i != (length - index - 1):
                    self.prepend(l[length - i - 1])
                else:
                    self.prepend(value)
I used a complex way to solve this, I m still not very understand the sample solutions' idea to write it, I hope TA can come back!

Saturday, 7 March 2015

Week 7: Recursion

I thought we do not need to write the slog in reading week, sorry about the late!

We have learned the class Tree and using recursive function to solve the lab exercise. To be honest, I feel like the first four functions: list_all, list_leaves, list_interior, list_if are not very difficult.  I used similar way to write list_all, list_leaves, list_interior. I constructed a list firstly, then use if and else statements to discuss the simplest situation and complex situation as showed below:  

def list_all(t):
    ''' (Tree) -> list
 
    Return a list of values in t.
 
    >>> t = Tree(0)
    >>> list_all(t)
    [0]
    >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
    >>> L = list_all(t)
    >>> L.sort()
    >>> L
    [0, 1, 2, 3, 4, 5, 6, 7, 8]
    '''
    L = [t.value]
    if t.children == []:
        return L
    else:
        for x in t.children:
            L = L + list_all(x)
        return L
 
def list_leaves(t):
    ''' (Tree) -> list
 
    Return a list of values in leaves of t.
 
    >>> t = Tree(0)
    >>> list_leaves(t)
    [0]
    >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
    >>> L = list_leaves(t)
    >>> L.sort() # so list is predictable to compare
    >>> L
    [3, 4, 5, 6, 7, 8]
    '''
 
    L = []
    if t.children == []:
        L.append(t.value)
        return L
    else:
        for x in t.children:
            L = L + list_leaves(x)
        return L
         
 
def list_interior(t):
    ''' (Tree) -> list
 
    Return a list of values in interior nodes of t.
 
    >>> t = Tree(0)
    >>> list_interior(t)
    []
    >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
    >>> L = list_interior(t)
    >>> L.sort()
    >>> L
    [0, 1, 2]
    '''
    L = []
    if t.children == []:
        return L
    else:
        L.append(t.value)
        for x in t.children:
            L = L + list_interior(x)
        return L
 
Notice that: in the list_all, I added the t.value immediately to the list I constructed, because we need to list all the values in t. 
 
However, for the function list_to_depth, I spend lot of time. Although I passed the test, I found a bug for the function. If put the >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 2)
and >>> L = list_to_depth(t, 4)on the python shell, the out put is not right.
so I changed the body of the function and wrote a helper function.
 
def list_to_depth(t, n):
    ''' (Tree, int) -> list
 
    Return a list of values in t from nodes with paths no longer
    than n from root
 
    >>> t = Tree(0)
    >>> list_to_depth(t, 0)
    [0]
    >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
    >>> L = list_to_depth(t, 1)
    >>> L.sort()
    >>> L
    [0, 1, 2, 3]
    '''
    test = True
    L = []
    if t.value == n:
        L.append(t.value)
        return L
    else:
        if n in [x.value for x in t.children]:
            test = False
            L = [t.value] + [x.value for x in t.children]
            return L
        else:
            L = [t.value] + [list_to_depth(x,n) for x in t.children if test]
            return list1(L)
 
def list1(L):
    '''
    Return the one dimension list of the nested list L. 
    >>> L = [2, [3, [5, 6]], 7]
    >>> list1(L)
    [2, 3, 5, 6, 7]
    l = []
    for i in L:
        if isinstance(i, int):
            l.append(i)
        else:
            l = l + list1(i)
    return l
 
As above, the helper function list1 is a function convert a nested list into the one dimension list. I have not find another simpler way to solve the function list_ to_depth.
 
I will discuss it with my friends and ask TA after striking.
 
 
 
 
 


Sunday, 1 March 2015

Week 8: My impressions of week 7

This week we learned the Class BTNode, which is a way to output a tree which every node only has two children. In this class, we use self. data, self.left and self.right to donates the node, two children respectively.

However, the most important part is the writing recursion, we use the recursion to calculate some values of the tree, such as the longest path or the number of nodes. The way to write recursion is similar as before, it is using if statement to calculate the simplest situation and else statement to write the function to call itself for the complex situation.

For me, I spend a lot of time to think about how to convert string '*' to the operation *, TA and me discuss with it, finally, he gives me a quick way to do that: eval(str(self.left)+self.data +str(self.right)), so that we can do not use four if statement to evaluate the operation of the self.data.

I'm so thankful for my TA, I heard that he will strike next week, I feel so sad. He is very patient guy; every lab, I ask him so much questions that I didn't understand in class.
Anyway, although the strike will influence us, we will still work hard.

Sunday, 15 February 2015

week 6: Tree Class( The summary of Object_Oriented Programming concepts

This week, we have learned how the Tree Class works and how to write some recursive function based on Tree and Queue.

Actually, I did not know the function descendents_from_list before lab this week. I'm so thankful my Ta of our lab, he told me that descendents_from_list function is a function to build a tree based on the arity. For example, the tree descendents_from_list(Tree(0), [1, 2, 3, 4], 2) is a tree starting with Tree(0) and listing the 2 nodes which is the same number as arity from the first two elements of the list; thereby, the 2 nodes will have the same number of children as arity if there were enough elements remained in the list. In the situation of descendents_from_list(Tree(0), [1, 2, 3, 4], 2), the tree starts with 0, two nodes tree 1 and tree 2, and tree 1 has two nodes 3 and 4.

Furthermore, in the lab exercise, I learned how to write a recursive function more deeply compared with last week. I know when the function called itself, it would start a brand new id location to store his value used in function body, and we need to 'building a place' to store the total useful value when we ran it.

I'm expecting next lecture! By the way, I feel recursion is very interesting and will be very useful in the further study.

Sunday, 8 February 2015

Week 5: the stack and queue.

This week we learned the class Stack and class Queue. These two classes actually are two way to store and remove elements of list or string.

For class Stack: we store things like filling something into a bottle, so the element added last was pulled out first. In other words, remove the element at the end of the list or string.

For class Queue: we store things like lining up a line so that the element added first was pulled first. In other words, remove the element at the beginning of the list or string.

In Lab#4, we have the exercise with the Stack and Queue. I think the hard part is storing the list in right arrangement so that the output of methods matches the requirements.Firstly, I used recursion function I learned last week to transform the nested list ([1, [2, 3], 4]) in to a simple list ([1, 2, 3, 4]). Then, we can use the simple list to be stored by Stack.push(). Similarly, we also need change the format of the input when we use the class queue. Secondly, we use input('Type...:') and while loop with __.is empty for both class to store the string and integer for class Stack and class Queue respectively.

I feel these two classes will be very useful in the future, let's see!

Sunday, 1 February 2015

Week 4: My Impression of Week 4

After a whole weekdays, we had studied so much new knowledge about Python. 

Firstly, I learned the differences between __repr__ and __str__.  __repr__ need to be implemented for any class you implement, sometimes working like 'self' input. Implement __str__ if you think it would be useful to have a string version which give the users information about errors or subclasses and so on. For example, the class named GameView(): then if __repr__ defined to be return GameView(), the input g.__repr__() on shell is a input. However, __str__ will give you information like : 'Requiring a subclass' provided that def __str__(self): return ('Requiring a subclass').

Secondly,  we studied a very important thinking in computer science -- recursion. Recursive thinking is a way that call the function itself to solve a complex and long problems. People maybe wanna ask why we should learn a hard way to code, the answer is that recursion can solve questions in elegant and concise way. On the week 4 lab, we traced a recursive function and write out an unknown doc-string of another recursive function. Nevertheless, the most challenging part is write a recursive function based on the doc_string and examples. I coded that if isinstance(L, list): return max([len(L), length(x) for x in L])   else: return 0. Actually, this function spend me 20 minutes, since it used len() and max() two existing functions so that hard to combine 2 functions. 

Anyway, it is a long way to learn recursion. Studying hard and get good mark. 

Saturday, 24 January 2015

Week 3: Why geeks should write?

Hello, everybody! welcome to my blog: youbujianle.blogspot.ca. Firstly, let me introduce myself. My name is Rong Wei, but I prefer people call me english name - Jane. I'm a second year student, specializing in Mathematics and its application in probability and majoring in Physics. The reason why I didn't take csc148 in first year is that I hadn't decide which program I interested in before.

In first year, I had studied in csc104, which is instructed by professor Danny Heap. I still remember that we all need to write slog every week. At that time, I didn't take it seriously. On the one hand,  I didn't find the efficient way to arrange my study schedule, so I always posted blogs very late. On the another hand, I did not totally understand the advantages of slog.

Now, I'm a second year student who already earned 9.5 credits, I feel writing slog is a good way to keep my knowledge about this course in track. Normally, Students take 5 courses every semester, but I'm having 6 courses now; as a result, I will forget the contents easily after one weekdays if there is no homework need to hand in. Obviously, writing slogs is an excellent way to address this problem.