Yeah~ Here comes the last week.
This week, we talked something about Big-Oh. It kinda reminds me of CSC165, but I think it studied further than what we learnt this week. I remember we discussed a lot for almost 3 weeks in CSC165 last semester. Big-Oh is like an index of complexity of the function. How many times the loop would be reached and how many lines overall would be reached. If there is one loop in a function and the parameter is all about n or kn(where k is a constant), then this function is in O(n). If there are two loops in a function and there is a loop inside the other, and the parameter of loop are all about n or kn, then it is in O(n**2). If there is a loop in a function and it is about n**2, then it is in O(n**2) as well. Overall, it is like a relationship between the power of parameter, the deepness of loops and how much time it needs to compute the function, and how quickly the time is increasing. What I did not know at CSC165 is O(lgn) is slower than O(n) and it doesn't matter whether the base is 10 or 2 because the difference between them is just constant.
I feel learning a lot in this semester. Finally, I would like to say 'Thank you' to Danny Heap and Diane Horton for what they've done in this semester! Good luck on final exams, my friend.
2015年4月5日星期日
Revist recursion
Today, I read my previous opinion about recursion. I found many misunderstanding at that time. I was thinking that recursion is something similar to a loop. That's totally wrong. Maybe iteration is more like a loop than recursion. What I was right at that time is about its cleanness and tidiness. Recursion is something that weird but beautiful. A recursive function is like a function or algorithm for all parts in a recursive structure such as tree. But as I learn more, I found it's not true that we can always write what a function needs to do in one function, maybe that's because what it needs to do becomes more complicated compare with the algorithms we learnt before. Just like some functions in Assignment 3. I found that, some functions in Assignment have recursive part and non-recursive part in one function. And since recursive part would be reached every time, we can not write the non-recursive part and recursive part together. For example, I need a empty list at the beginning to be used to append elements, and the algorithm of appending elements is recursive. If I put assignment statement in recursive part. At each step, an empty list will appear and there is no way to append it to the original one. So instead, I assign an empty list in the function and write a help function which is recursive to let it do its job, and then I pass the list in recursive function parameter. The helper function do nothing but appending suitable elements to the list in parameter.
I feel as I learn more about recursion, I feel more uncertain about whether recursive part has a typical format. I thought recursive functions would always be like:
if XXXXX:
XXXXXX
else:
XXXXXX
However now I know it is not like that. Recursive functions do have if statement and else statement, but they depend upon the node it is working on is a leaf or node. Sometimes we do not need to add an else statement because we need it to do something even after it finishes if statement. For example, the if statement turns a leaf into a node and then if we want to do something to the nodes, we don't need to add an else statement.
I feel as I learn more about recursion, I feel more uncertain about whether recursive part has a typical format. I thought recursive functions would always be like:
if XXXXX:
XXXXXX
else:
XXXXXX
However now I know it is not like that. Recursive functions do have if statement and else statement, but they depend upon the node it is working on is a leaf or node. Sometimes we do not need to add an else statement because we need it to do something even after it finishes if statement. For example, the if statement turns a leaf into a node and then if we want to do something to the nodes, we don't need to add an else statement.
2015年4月3日星期五
Week 9
This week, we had a term test and I did not do well in the test. Especially in part B and part C. I guess the reason is that I put too much time in Assignment 2 and didn't prepare well for this coming test. Part B is mainly about Binary Tree. The function is list_even_between, that is a function return all even integers in the range given by start number and end number. I practiced this kind of functions in labs but I did't understand them fully at that time. And in part C, I forgot to check whether the data it return exists.
And because the test, we have only 1 lecture this week. It is about the insertion function and the delete function in a binary search tree. I think from now on, we have entered a new stage which require more advanced recursive thinking. Sometimes, I feel I just can't figure out what it should be, but when I see the answer, suddenly I understand how it works.
I would like to talk more about the delete function. The algorithm is like:
1. If this node is None, return that
2. If data is less than node.data, delete it from left child and
return this node
3. If data is more than node.data, delete it from right child
and return this node
4. If node with data has fewer than two children,
and you know one is None, return the other one
5. If node with data has two non-None children,
replace data with that of its largest child in the left subtree,
and delete that child, and return this node
At the beginning, I don't understand the return part in 4 and 5. After drawing some pictures, everything becomes clear. It's like if you cannot find anything on one side, then start searching the other side.
And because the test, we have only 1 lecture this week. It is about the insertion function and the delete function in a binary search tree. I think from now on, we have entered a new stage which require more advanced recursive thinking. Sometimes, I feel I just can't figure out what it should be, but when I see the answer, suddenly I understand how it works.
I would like to talk more about the delete function. The algorithm is like:
1. If this node is None, return that
2. If data is less than node.data, delete it from left child and
return this node
3. If data is more than node.data, delete it from right child
and return this node
4. If node with data has fewer than two children,
and you know one is None, return the other one
5. If node with data has two non-None children,
replace data with that of its largest child in the left subtree,
and delete that child, and return this node
At the beginning, I don't understand the return part in 4 and 5. After drawing some pictures, everything becomes clear. It's like if you cannot find anything on one side, then start searching the other side.
2015年4月1日星期三
Week 8
Finally the due day of Assignment 2 comes. After submitting Assignment 2, week 8 comes and we are about to start Linked List and mutation.
Linked List is a kind of Data Type. It looks similar to tree but has a significant difference, that is, it doesn't need recursion, but instead, it uses iteration. And it is also more efficient than a tree. Finally, it needs a wrapper, for which the beginning of it point at the beginning of Linked List and the 'tail' of it point at the current LinkedListNode.
And we learnt mutation as well. To me, mutation is something like modifying itself without returning something. It somehow remind me the time when I was learning list. At first, I was totally confused about the list type. I don't understand why it can change itself without return a new list. I was thinking that every function would return something but I found myself wrong.
I would like to say something about TA strike. I find it is very difficult to learn lab material without TA's help. Before the strike, TA explained every step to us at the end of each lab but now we need to learn these material by ourselves. I hope the strike will settle down and they will come back as soon as possible.
Linked List is a kind of Data Type. It looks similar to tree but has a significant difference, that is, it doesn't need recursion, but instead, it uses iteration. And it is also more efficient than a tree. Finally, it needs a wrapper, for which the beginning of it point at the beginning of Linked List and the 'tail' of it point at the current LinkedListNode.
And we learnt mutation as well. To me, mutation is something like modifying itself without returning something. It somehow remind me the time when I was learning list. At first, I was totally confused about the list type. I don't understand why it can change itself without return a new list. I was thinking that every function would return something but I found myself wrong.
I would like to say something about TA strike. I find it is very difficult to learn lab material without TA's help. Before the strike, TA explained every step to us at the end of each lab but now we need to learn these material by ourselves. I hope the strike will settle down and they will come back as soon as possible.
订阅:
博文 (Atom)