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.
CSC148 SLOG
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.
2015年3月13日星期五
Week 7
Hey, this week, I want to talk about Assignment 2, which was to design a strategy and a new game state. This assignment is more difficult than Assignment 1.
First, let's talk about Minimax. In assignment 1, we were told to design a random choosing strategy, so we only need to import random and choose a possible move from the list. But Minimax is a strategy such that computer will try to win and if there is a chance to win, it will definitely win. So it is a recursive strategy and I don't think there's other way to implement besides using recursive structure. After figuring out its structure, here comes the most difficult part, writing code. I read the handout very carefully and find the picture interesting. It is basically saying that we need to consider all possible moves and all states can be derived from that move, so it is quite obvious that some of them will end with A win or B win. If A wins, then, the it return 1 to the last state will made the move. So when we reach the node, we will always try to make a decision of which one to choose, given that all of them end up with a winning condition. I was thinking if we need to compute how complex each subtree is and choose the simplest tree, the code becomes very long and tedious. Besides, it may take computer very long time to reach leaves and again compute complexity of every subtree at the very beginning. So I decided to choose first possible move that maybe end up with a winning condition.
Second, that's about TippyGameState. Tippy is a very interesting game, very different from Tic-Tac-Toe. The winning condition is somehow weird but in this way, random strategy and Minimax strategy are more useful. If what we implement is Tic-Tac-Toe, it is pretty likely game end with a tie. The winning condition of Tippy reduces this possibility and makes it more challenge. However, implementation of detection of winning block is not that simple. I worked out an algorithm that using a for loop and for very coordinate that has an 'X' or 'O', I look at its surrounding for specific shape. If there is , return True. Else return False. It is like:
for coord:
if <some condition>:
return True
elif <some condition>:
return True
elif <seom condition>:
return True
return False
I have to say this assignment is interesting and challenging, especially for those worked solo like me. I am thinking of working in partner next time.
First, let's talk about Minimax. In assignment 1, we were told to design a random choosing strategy, so we only need to import random and choose a possible move from the list. But Minimax is a strategy such that computer will try to win and if there is a chance to win, it will definitely win. So it is a recursive strategy and I don't think there's other way to implement besides using recursive structure. After figuring out its structure, here comes the most difficult part, writing code. I read the handout very carefully and find the picture interesting. It is basically saying that we need to consider all possible moves and all states can be derived from that move, so it is quite obvious that some of them will end with A win or B win. If A wins, then, the it return 1 to the last state will made the move. So when we reach the node, we will always try to make a decision of which one to choose, given that all of them end up with a winning condition. I was thinking if we need to compute how complex each subtree is and choose the simplest tree, the code becomes very long and tedious. Besides, it may take computer very long time to reach leaves and again compute complexity of every subtree at the very beginning. So I decided to choose first possible move that maybe end up with a winning condition.
Second, that's about TippyGameState. Tippy is a very interesting game, very different from Tic-Tac-Toe. The winning condition is somehow weird but in this way, random strategy and Minimax strategy are more useful. If what we implement is Tic-Tac-Toe, it is pretty likely game end with a tie. The winning condition of Tippy reduces this possibility and makes it more challenge. However, implementation of detection of winning block is not that simple. I worked out an algorithm that using a for loop and for very coordinate that has an 'X' or 'O', I look at its surrounding for specific shape. If there is , return True. Else return False. It is like:
for coord:
if <some condition>:
return True
elif <some condition>:
return True
elif <seom condition>:
return True
return False
I have to say this assignment is interesting and challenging, especially for those worked solo like me. I am thinking of working in partner next time.
2015年3月1日星期日
Recursion
This week's topic is chosen among three assigned topic. And I chose recursion because I was surprised by its beauty and efficiency, along with difficulty of implementing. In this slog, I will try to demonstrate some recursive structure and how it is different from looping structure.
It is my very first time to notice recursive structure when I read the structure with self inside. At that time, it was a function about nest list. Before learning recursion, I always wondered how to reach the element in a nest list with different 'depth', let's say [1, 2, 3, 4, [5, [6, 7]]], I can surely point out there are seven elements in this nested list. However, for computer, it is a list with five elements in it, and it is not true that every element is a list with elements in it. So, my first impression is we need a if statement and an else statement to differentiate these situations. At that time, I thought the code would be like this:
i = 0
if #element is a list:
#count the element in this list
for num in element:
i += 1
elif #element is not a list:
#i += 1
But later on, I find that it only works with list of 'depth' 2, if we have a complicated list such as [1, 2, 3, [4, 5, [6, [7]]]]. Then the situation is the num in the function is a list as well. So, obviously, using a loop might enable us to solve the problems with knowing how deep the list is. The problem, however, can be very sophisticated and we may not be able to always find the depth of 'target list'.
At that time, it was about week 4, Danny introduced us the solution to nested list, a recursive function. It is to sum up all numbers in a nested list. What surprised me is code, it is quite concise and tidy. The code is like:
def sum_list(L):
'''
# omited
'''
if isinstance(L, list):
return sum([sum_list(x) for x in L])
else:
return L
At that time, I didn't know list comprehension, so I cannot figure out what this code really does. There is only one place where sum() shows, that is if L is a list, but why there appears else, and if we need a else statement, In what kind of situation do we need to use it? I have so many question. In the following weeks, Danny let us to trace recursion, first example was always pretty simple, which is base case that wouldn't use recursive part. When it comes more complicated part, I feels like can understand why code is like that, but cannot write out this code by myself.
It is my very first time to notice recursive structure when I read the structure with self inside. At that time, it was a function about nest list. Before learning recursion, I always wondered how to reach the element in a nest list with different 'depth', let's say [1, 2, 3, 4, [5, [6, 7]]], I can surely point out there are seven elements in this nested list. However, for computer, it is a list with five elements in it, and it is not true that every element is a list with elements in it. So, my first impression is we need a if statement and an else statement to differentiate these situations. At that time, I thought the code would be like this:
i = 0
if #element is a list:
#count the element in this list
for num in element:
i += 1
elif #element is not a list:
#i += 1
But later on, I find that it only works with list of 'depth' 2, if we have a complicated list such as [1, 2, 3, [4, 5, [6, [7]]]]. Then the situation is the num in the function is a list as well. So, obviously, using a loop might enable us to solve the problems with knowing how deep the list is. The problem, however, can be very sophisticated and we may not be able to always find the depth of 'target list'.
At that time, it was about week 4, Danny introduced us the solution to nested list, a recursive function. It is to sum up all numbers in a nested list. What surprised me is code, it is quite concise and tidy. The code is like:
def sum_list(L):
'''
# omited
'''
if isinstance(L, list):
return sum([sum_list(x) for x in L])
else:
return L
At that time, I didn't know list comprehension, so I cannot figure out what this code really does. There is only one place where sum() shows, that is if L is a list, but why there appears else, and if we need a else statement, In what kind of situation do we need to use it? I have so many question. In the following weeks, Danny let us to trace recursion, first example was always pretty simple, which is base case that wouldn't use recursive part. When it comes more complicated part, I feels like can understand why code is like that, but cannot write out this code by myself.
2015年2月17日星期二
Summary of Object-Oriented Programming
In the last class of CSC108, we began learning Object-Oriented Programming. The instructor introduced us of class and object. At that time, I was totally confused by Object-Oriented Programming and Procedure-Oriented Programming and cannot tell the difference between them. But after learning CSC148 for a couple of weeks, I realize how different they are.
In first week's lesson, Danny used a turtle example to show us what is OOP. To me, it seems that everything in Python is an object, a function is an object, a list is an object and even a class is an object. And unlike Procedure-Oriented Programming language such as C, python define a function instead of declare the output type, so basically it is creating a function instead of defining a procedure.
Then, we learnt some methods of object such as __eq__, __str__ and __repr__. At the first time, I don't know when to use __str__ and when to use __repr__. Danny showed us an example of Point. The only difference between the outputs of str and repr is the class name 'Point', which is for python to evaluate. Therefore, __repr__ is an official representation of the object and can be evaluated by python to give back the object while the __str__ method is used to produce a human-readable string of that particular object. __eq__ is simpler compare to the two methods before, it is more like giving the programmer a way to define what properties do two objects need to be called 'equal'.
Afterwards, we learnt the property methods, which is used by programmer to prevent users from modifying important data. It is somehow more interesting to me than the concepts I've learnt before because it seems that only Python has such a method for programmers to protect the data.
In first week's lesson, Danny used a turtle example to show us what is OOP. To me, it seems that everything in Python is an object, a function is an object, a list is an object and even a class is an object. And unlike Procedure-Oriented Programming language such as C, python define a function instead of declare the output type, so basically it is creating a function instead of defining a procedure.
Then, we learnt some methods of object such as __eq__, __str__ and __repr__. At the first time, I don't know when to use __str__ and when to use __repr__. Danny showed us an example of Point. The only difference between the outputs of str and repr is the class name 'Point', which is for python to evaluate. Therefore, __repr__ is an official representation of the object and can be evaluated by python to give back the object while the __str__ method is used to produce a human-readable string of that particular object. __eq__ is simpler compare to the two methods before, it is more like giving the programmer a way to define what properties do two objects need to be called 'equal'.
Afterwards, we learnt the property methods, which is used by programmer to prevent users from modifying important data. It is somehow more interesting to me than the concepts I've learnt before because it seems that only Python has such a method for programmers to protect the data.
订阅:
博文 (Atom)