Thursday, 2 April 2015

SLOG #10 SOME FINAL WORDS

It seems like this school year is about to end, time has really flown by.
I can still clearly remember the first day of CSC148 at the beginning of the semester. I was sitting in the first row of the class, listening attentively to Diane's introduction to the course as well as to herself, eager and expecting to learn a lot from this class.  At that time, all I knew about the subject was how to program in python; I had no idea why and how it can be called a "science". As the semester went through, I was exposed to more and more concepts in computer science, and began to have an obscure idea in my mind what the subject is really about. Now at the end of this class, I can confidently say that CSC148 has definitely lived up to my expectation. I understood how sophisticated and powerful computer science is, as well as a lot of practical skills in programming Moreover I am applying what I have learn in this class to other scientific fields I am studying ( I am a statistics specialist, computer science is very helpful in statistical computing and simulations). I have to say that this is one of my favourite courses that I have taken in U of T ( the other two are PSY100 and MAT257)!

As Diane has mentioned in the last slide: although CSC148 has finished, the study of computer science has just begun. This course has triggered my curiosity to take more courses in the theory part of computer science. I am planning to take CSC265 next semester. I know it is going to be challenging, but through two years of university experience I have learnt that it is almost always better to challenge yourself than to choose the easy way. Although I do not have CSC165 and CSC236, I think my strong mathematical maturity can be a substitute of these two prerequisites.

The finial is coming at the end of this month. I should start to prepare for it once I have finished preparing for my other exams that will happen before it. In specific, I am going to review the lecture slides and codes that we went through in the class, re-do some lab exercises, and most importantly, do practice questions and past exams. With these preparations, I am confident that I will succeed in the final exam!






Friday, 27 March 2015

SLOG #9 SOME RETROSPECTION

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.

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!

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.

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:
  1. The function is well defined at a base case or cases 
  2. 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.
It is not hard to see that the function is well defined on all values starting from the base case (usually natural numbers), since for each number it gives the computer an algorithm or "roadmap' to instruct it what to do. One advantage of recursion is that the programer only needs to do two things: define the function at a base value, and tell the function to refer to its previous value when given a number that is not the base value. Sometimes this feature of recursion can be significantly more convenient (although it may take the computer a very long time to trace back to the base case). For example, with recursion, it is very easy to define factorial, or to evaluate the nth term of Fibonacci sequence. On the contrary, using closed form formulas for the above problems might be much faster, but they are quite complicated: the closed form of factorial involves gamma function, where we need to numerically evaluate an improper integral; and the formula for Fibonacci sequence has exponential terms; both of which will return a float instead of int. Hence when input size is large, floats from closed formulas might be far from the real values.

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.

Monday, 16 February 2015

SLOG #4 STACKS, QUEUES, AND TREES

Although the prescribed topic of this week's slog is a summary of Object-Oriented Programming concepts, I will mainly talk about the material I learnt from last week and this week's labs since I have mentioned OOP in a previous slog.

I found what I have learnt in the past two labs particularly helpful, partly because Diane only mentioned the abstract data types (ADTs): stacks, queues, and trees briefly in class. In the labs, we were required to read through the codes of these data structures and write methods or functions for them, which gave me a more solid understanding on how they work and how we can make use of them. We also learnt some applications of these ADTs. For example, we can use a queue to represent a queue in a restaurant, or we can use a tree to trace the order of a sequence of binary operations.

The materials in lab #4 is not very difficult, but it becomes quite tricky when I tried to implement recursive functions on trees in lab #5. It is easy to think of a base case(when the problem can be solved in one single step). In code, this is usually the "if" part. However, I got lost when I started trying to write something in the "else" part. Fortunately, the lab handout gave me some hints; it asked me look at a larger tree. Assuming that the function works on its subtrees, what should I do to make the function also work on this larger tree? This is very similar to the induction step in an induction. In induction, you prove the base case, assume the statement holds for all numbers smaller than a number k, and prove the statement also holds for k, using the above facts. In induction, we know our function works for the base case, we assume the function works for any level of input smaller than a certain number k(in this is it is the height of a tree), and we construct the function such that it will work for level of input k. Thinking in this way, the problems in the lab were greatly simplified; all I needed to do was to think of base case, and design function so that it works for trees with height two. Thanks to recursion, the subtrees collapsed to the leaves that are directly connected to the root!

Reading week is coming, so is assignment 2. Hopefully I will be able to have a draft for A2 by the end of this week.

Saturday, 7 February 2015

SLOG #3 MERGE SORT, AND THE MIDTERM

Last week we started our first lesson on recursion, an extremely powerful tool for computer scientist. So far, I only have a brief and vague idea of what recursion is, and I feel writing recursive function quite tricky(the last question in last week's lab). I attribute this feeling to a lack of exposure to this subject, so I will reserve it to the slog later in the semester that is prescribed to have this topic. Hopefully by that time I will feel more comfortable writing recursive functions.

The other topic that we covered last week was merge sort. Diane did not show us the code for this algorithm in the class. Instead she brought eight playing cards to show us how merge sort works. She first divided the cards into two groups, each group having four sorted cards. Then she put the sorted cards in two piles, held the cards at the top of each pile of cards, compared them, and put the card with smaller face value down on another area of the table. By doing so continuously, she completed the sort within four comparisons, suggesting that this way of "dividing and conquer" might be efficient in sorting lists. Then we had this vivid presentation of merge sort: Diane handed out the eight cards to a student in the first row. That student divided the cards into two piles, giving each pile to a person in the second row. The students in the second row and third row did the same thing until each of the eight students of the fourth row had one card. This is the "divide" part. The "conquer" part starts with students in the fourth row passing back the cards to the person in the third row, and that person needed to sort the cards. This is easy because these students only need to sort two cards. Now we have four sorted groups of cards. Apply this process again, we have two sorted groups of cards. Apply this process for the last time, we have a sorted list!

It is notable that the whole process consists of three "divide", and in each of the three sorting parts, we made four comparisons. This might be much faster than insertion sort if the list is not originally well sorted, in which case the run time may be as many as 64 times!

We also had a midterm this Wednesday. I have to say that the test was fair. Nothing really unexpected: two question about the design and implementation of class, the second of which tests on subclasses, and one that asks you to trace a recursive function. I don't find anything that is particularly tricky in these questions. Hopefully I did well in the midterm.

Saturday, 31 January 2015

SLOG #2 A FEW THOUGHTS ON OBJECT-ORIENTED PROGRAMMING

In the first three weeks of the class, we learnt how to instantiate a class and define methods to the class. The instance variables, or attributes of the class is an example of objects, which is the core of the object-oriented programming.

At the first exposure to OOP, I was quite confused by its functionality. I got quite frustrated when I was trying to solve assignment one; we were required to use classes to make a game, but I can make exactly the same game with the functions and data types I learned in CSC108 and it is easier. The process of programming is after all the process of thinking, but thinking in an object-oriented way didn't quite fit my intuition. I did understand that defining new data types and storing data in classes can be handy in many situations, but the excessive usage of class in assignment one seemed to be artificial and unnecessary.

After I talked to one of my CS specialist friends about what I thought about classes and OOP, he explained to me why they are important in the design of a program. In first year computer science classes, projects usually do not have a large size, so collaboration is not essential. But as the thing we want to build get bigger and bigger, one person cannot possibly finish it on herself, or if we want to make something that other people can extend or modify, OOP is extremely useful. This is because that one of the most important feature of OOP is that data are accessible and usually modifiable by users and programmers. Moreover, a parent class provides roadmaps to the design of subclasses, so if other programmers want to extend the class for more functionalities, they will follow the instruction from parent classes or simply inherit methods from them. For example, in assignment one, making a class for generic game and generic strategy saves us from the trouble of writing a new file for each specific game and strategy.

Sunday, 25 January 2015

SLOG #1 WHY GEEK SHOULD KNOW HOW TO WRITE

It is almost ostensible that being able to code and design algorithms are the most important skills for a computer scientist. However, one can hardly argue that the ability to write, in English, not programming languages, is of less significance than the above expertise. English writing is crucial for computer scientists, as it is for virtually all the other fields of professions, for the following reasons:
 
1. Writing is important for computer science pedagogy. No one would claim that her native language is java or python. Instead, a novice can only acquire the language of computer science through learning in the language they use every day. If a computer science professor wants to deliver her knowledge to her students, she must be able to articulate the ideas, which might be quite complex, in her speech or writing. Clarity, accuracy and efficiency in expression make it easier for students to grasp the material in the class.

2. Communication is essential in teamwork. A lot of computer science students end up becoming software engineers. The size and complexity of the work of software engineers makes working on one's own very difficult, if not impossible. As a result, they usually work in teams. A team could be very large depending on the project. If the crews in the team are not efficient in communicating with each other, they may have hard time putting their work together to achieve the goal of their project. So a team will not be productive if the communication between members of the teams efficient. The efficiency of communication, of course, relies on the person's proficiency in writing.

3. Sharing helps promote ideas. Sometimes geeks design softwares that can be utilized by or programmable to other computer scientists. In order to promote these softwares, the geeks should instruct the potential users of how they work and persuade them to use it. Even if this software is extremely well made, without a proper promotion, which largely depends on what the geeks say and write about it, it may not become popular among the users. As Joel Spolsky said, "the programmers with the most power and influence are the ones who can write and speak in English clearly, convincingly, and comfortably."