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, 29 March 2015
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!
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.
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:
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 Ldef 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.
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.
Subscribe to:
Posts (Atom)