Going through my previous slogs, I find that I agree to myself to all of the specific topics that I wrote about. I think this is partly because I would post a slog only after I carefully think about the problem or the topic that I was about to post. Nevertheless, my thoughts and knowledge about computer science in general have certainly evolved as the class proceeds. It is now much more mature and systematic than it was at the beginning of the class. And from what I have read from some of my fellow classmates' slogs I think many of them have the same feeling as I do.
I consider these changes in thoughts and ideas of computer science a progress. To be specific, I list some of these changes below.
1. At the beginning of this course, I had a very vague idea of the difference and connections between programming and computer science. Now I understand that programming is the application side of computer science as a scientific discipline. Computer sciences provides programmers and software engineers better and faster algorithms to solve problems and make softwares. Abstract data types and complexity analysis are essential in coming up and implementing these algorithms.
2.Although after assignment 1, I had already had some obscure ideas about why class is very important in software development, it was only after assignment 2 and some other practice on my own did I fully (I think) understand classes. The starting code of A2 exemplified how classes encapsulate different parts of codes so that it is easier to modify or add content to them. I also strengthened my understanding of class objects by downloading and reading python packages as well as the code of abstract data types, which are usually instantiated in classes.
Enough said, I am quite satisfied that I took this course as a mathematics and statistics student. From my perspective, this is one of the most useful courses that I have taken in U of T.
Friday, 27 March 2015
Sunday, 22 March 2015
SLOG #8 UNIT TESTING AND BIG-OH
We only covered one topic this week: unit testing. Being a mathematics specialist, I am more interested in the science part of of computer science (like data structure and algorithms), not developing, so I did not pay much attention to unit testing. Despite this fact, as a student who used to take class in programming (in particular csc108), and who is currently using python to implements his projects in other sciences, I am aware of the significance of testing.
It is fairly common that a piece of wrong code passes doc test. In this case, unit testing can be very useful in determining the correctness of the code as well as identifying the bug, especially when we have good test cases. And by good test cases I mean the ones where wrong codes are very unlikely to pass the tests. In practice, we use the method assert to implement unit testing. Method assert takes on two arguments: a boolean value which is usually used to tell us wether the function being tested gives us what we want, and a string that gives us an description of the error when the boolean value is False. Assert can also be useful in many other cases. For example, we can put assert in critical points in a function body to see if the function is correct up to this point, and if the code is wrong, it is easier to identify the bug because we now have more information about it from assert method.
Realizing that the length of this slog is relatively short and that we have a prescribed topic for slog week 11, I also include some of my thoughts about the material taught in week 11: algorithm complexity and big-oh notation.
In class, we compare the performance of various sorting algorithms over a list of integers of given sizes. In particular, we analyzed the run time of these algorithm as a function of the size of the list being sorted (denote as n). For example, merge sort first divide the list in a binary manner, which takes approximately log(n) times, and for each level of division, the sorting process takes exactly n run times. As a result, merge sort takes approximately nlog(n) run times to sort a list of integer with size n. We did not actually give a definition of big-oh notation. In mathematics, big-oh describes the behaviour of a function when its argument tends to a particular value. In computer science, this value is usually infinity, meaning that big-oh describes the approximate run time of an algorithm when the input size is very large!
It is fairly common that a piece of wrong code passes doc test. In this case, unit testing can be very useful in determining the correctness of the code as well as identifying the bug, especially when we have good test cases. And by good test cases I mean the ones where wrong codes are very unlikely to pass the tests. In practice, we use the method assert to implement unit testing. Method assert takes on two arguments: a boolean value which is usually used to tell us wether the function being tested gives us what we want, and a string that gives us an description of the error when the boolean value is False. Assert can also be useful in many other cases. For example, we can put assert in critical points in a function body to see if the function is correct up to this point, and if the code is wrong, it is easier to identify the bug because we now have more information about it from assert method.
Realizing that the length of this slog is relatively short and that we have a prescribed topic for slog week 11, I also include some of my thoughts about the material taught in week 11: algorithm complexity and big-oh notation.
In class, we compare the performance of various sorting algorithms over a list of integers of given sizes. In particular, we analyzed the run time of these algorithm as a function of the size of the list being sorted (denote as n). For example, merge sort first divide the list in a binary manner, which takes approximately log(n) times, and for each level of division, the sorting process takes exactly n run times. As a result, merge sort takes approximately nlog(n) run times to sort a list of integer with size n. We did not actually give a definition of big-oh notation. In mathematics, big-oh describes the behaviour of a function when its argument tends to a particular value. In computer science, this value is usually infinity, meaning that big-oh describes the approximate run time of an algorithm when the input size is very large!
Friday, 13 March 2015
SLOG #7 MIDTERM 2 AND BST MUTATION
We had our second term test on Wednesday. Thanks to Diane, Danny, and Mike, the test was not cancelled. Overall, I think the second midterm is harder than the first one: the first question tests our knowledge about binary tree; the second and the third question asks us to write functions on binary search trees and linked lists. And I think it is harder than the first one mainly because of the last two questions.
The first question is not very tricky, one can figure out the minimum and the maximum of the height of a tree just by drawing pictures. I forgot the definition of pre-order traversal during the test, but I assumed it went in the order: data -> left -> right. I was quite lucky that I made this assumption and it gave me the right answer. The second one is more complicated. We need to use the properties of binary search tree to find out the even numbers that is within a given range of numbers, visiting as few nodes as possible. This isn't much of an obstacle to me because we have seen similar examples both in class and in labs, and I did a question from past test the night before the exam which is essentially the same question (except that on does not ask you to check even numbers). The third question is the hardest one in this midterm. As I mentioned in a previous slog, I found methods and functions on linked lists quite tricky, but I feel more and more comfortable implementing them as I practice more about it (mainly from labs and occasionally past exams). I proceeded the question in the following order: 1.figure out the corner case(s) and the general case; 2.draw graphs representing corner case(s) and a general case; 3. write the code for corner case with the help of the graph I drew; 4.write the code for general case with the help of the graph I drew; 5.see if I can incorporate the two cases together; 6.check if all instances are updated properly(front, back, size). With this procedure, I think I gave the right answer (or should I say a right answer?) to the third question.
As to the material we learnt this week; we only had one hour of class this week, and the announcement took almost one third of the class time (as always), so we briefly talked about the insert and delete method of binary search tree. These two methods are not quite different from the methods/functions on binary (search) trees we learnt before, so I am not going to talk much about them. One difficulty about delete is that when we try to delete an interior node with some children, how should we decide which node or value should take place of the deleted node as a child of the deleted node's parent? In class, Diane gave us a solution; promote the leaf that is the closest to the middle to that position. It is not hard to check that this action will not break the properties of BST. I also find this week's lab enlightening: it illustrates how the order matters when you insert a list of number into a binary search tree. If you just insert the list [1,2,3...500] in order to a BST. The resulting tree will have height 500; all the interior nodes only have left child. In this case, the BST does not actually reduce the search time compared to the original list. But if we shuffle the original list, the height of the tree can be reduced as low as 8 (if my calculation is not wrong), which increases the search efficiency.
The first question is not very tricky, one can figure out the minimum and the maximum of the height of a tree just by drawing pictures. I forgot the definition of pre-order traversal during the test, but I assumed it went in the order: data -> left -> right. I was quite lucky that I made this assumption and it gave me the right answer. The second one is more complicated. We need to use the properties of binary search tree to find out the even numbers that is within a given range of numbers, visiting as few nodes as possible. This isn't much of an obstacle to me because we have seen similar examples both in class and in labs, and I did a question from past test the night before the exam which is essentially the same question (except that on does not ask you to check even numbers). The third question is the hardest one in this midterm. As I mentioned in a previous slog, I found methods and functions on linked lists quite tricky, but I feel more and more comfortable implementing them as I practice more about it (mainly from labs and occasionally past exams). I proceeded the question in the following order: 1.figure out the corner case(s) and the general case; 2.draw graphs representing corner case(s) and a general case; 3. write the code for corner case with the help of the graph I drew; 4.write the code for general case with the help of the graph I drew; 5.see if I can incorporate the two cases together; 6.check if all instances are updated properly(front, back, size). With this procedure, I think I gave the right answer (or should I say a right answer?) to the third question.
As to the material we learnt this week; we only had one hour of class this week, and the announcement took almost one third of the class time (as always), so we briefly talked about the insert and delete method of binary search tree. These two methods are not quite different from the methods/functions on binary (search) trees we learnt before, so I am not going to talk much about them. One difficulty about delete is that when we try to delete an interior node with some children, how should we decide which node or value should take place of the deleted node as a child of the deleted node's parent? In class, Diane gave us a solution; promote the leaf that is the closest to the middle to that position. It is not hard to check that this action will not break the properties of BST. I also find this week's lab enlightening: it illustrates how the order matters when you insert a list of number into a binary search tree. If you just insert the list [1,2,3...500] in order to a BST. The resulting tree will have height 500; all the interior nodes only have left child. In this case, the BST does not actually reduce the search time compared to the original list. But if we shuffle the original list, the height of the tree can be reduced as low as 8 (if my calculation is not wrong), which increases the search efficiency.
Saturday, 7 March 2015
SLOG #6 ALL ABOUT LINKED LISTS
It seems the second midterm is coming up next week. Since the first midterm, we have learnt recursion, and abstract data types(stacks, queues, trees, and linked lists). I think the second midterm will consist of questions mostly from these topics. Luckily, I have grasped the notions and implementations of recursion and most of the abstract data types quite well from the labs. However, I find linked lists, what we are learning this week, quite tricky.
Diane introduces two classes to represent linked lists: LLNode, and LinkedList. LLNode is a node of a linked list. It has instance variables: value and nxt: value is the object stored in the current linked list node, and nxt points to the next node. While LLNode represents only one node of the whole linked list, the class LinkedList gives us a more comprehensive view of a linked list. An LinkedList object has three attributes: front is the first node in the linked list, back is the last node in the linked list, and size is the number of nodes in the linked list. Graphically, a linked list looks like this:
Although a linked list seems to be much simpler than a tree. But it turns out that for me, implementing functions and methods of linked list is difficult; it just doesn't work as I expected. I noticed this when I was trying to solve the lab of this week. I thought there would be a tutorial this week so I went to cdf lab on Thursday at 9 am. I only saw other two csc148 fellows in the lab so I knew the tutorial was cancelled. But I did not leave. Instead I tried to solve the questions in the lab handout as usual, thinking it won't take long before I finish implementing all the functions and methods. But after I spent more than 40 minutes trying to make the special method __add__ work, I got paranoid because the algorithms I had in my mind does not work when I typed them in code, and I could not figured out what was wrong. I didn't have time to finish all the functions on that day, possibly because I didn't actually follow the instruction to draw the graphs as required in the lab handout but rather picture linked lists in my mind. I'm going to try again after I finish this slog, and I will draw pictures to help my thinking. Hopefully this will work out.
Sunday, 1 March 2015
SLOG #5 RECURSION, AND ABSTRACT DATA TYPES
Time flies; half of the semester has past. I think I am ready to give a summary to what I have learnt in CSC148 for the past 6 weeks. Object-Oriented Programming, recursion, and abstract data types (ongoing) are the main topics of this course so far. Since I have already mentioned OOP in a previous slog, and since I now feel quite comfortable with implementing classes, I would talk (more) about recursion and abstract data types in this slog.
Recursion:
The defining properties of recursive functions are the following:
- The function is well defined at a base case or cases
- When the function is called at a point (usually a number) that is different from the base case, the function refers to the value of itself at the previous number. This process continues until the function hits the base case.
Another advantage of recursion will lead to my second topic today: Abstract Data Types. Among the ADTs we are learning are stacks. queues, and trees. I view ADTs as mathematical representations/models for certain objects that we might be interested in. For example, the picture below shows how a binary tree represents an arithmetic expression:
The structure of trees makes recursion an important tool when we want to implement methods/functions on this data type.
Last week, we began to study binary tree. As its name suggests, nodes in binary trees have as most two children. Unlike general trees which store their children in a list, binary tree nodes store their children separately as two instance variables. The leaves of a general trees have child empty list, while the leaves of binary trees have child None. This distinction makes the implementation of recursive functions on binary trees different: for general trees, we start with a base case where the tree has height 1, but for binary trees, we start with a base case where the binary tree is None. This prevent us from calling incorrect methods on NoneType. The exercise in this week's lab helped me understand binary tree more clearly.
As to assignment 2, I find implementing tippy game state not very hard, since I have been practicing more on OOP, and since the starter code provide a paradigm that I can refer to. We are required to implement a recursive function in strategy minimax. Given the practice in lab #5, this did not cause me much trouble.
In addition, I found the slog of this fellow CSC148 student from last year quite helpful and inspiring:
Please do leave a comment if you are reading this post.
Subscribe to:
Posts (Atom)
