Sunday, March 23, 2014

Week 10 - Assignment 2 Part 1

It is probably seems to be a bit late to write about Assignment 2 Part, since it had been like 2 weeks and Part 2 was already due. Yet, Part 1 seems to be hard to start compare to Part 2, and this is the reason why I want to write about this.
 Assignment 2, overall, introduced a very interesting concept of organizing linked lists and classes of string:
“Regular expressions (abbreviated to regex, the pronunciation of which gives rise to endless ame wars. . . ) are used in various programming languages and utilities to match entire classes of strings. This assignment will give you experience modelling a regular expression as a tree, and detecting which strings match a given regular expression.”                           
                                                                 --- (Assignment #2 Handout)
                                http://www.cdf.toronto.edu/~heap/148/W14/Assignments/A2/a2.pdf
In part 1, we simply just have “design a collection of classes to represent the various sorts of regular expression trees. Each class should implement or inherit an __eq__ method so that it can be compared to other objects; and a __repr__ method so that it can be represented in a meaningful way as a string, and so that an equivalent tree will be produced if you cut-and-paste the representation into a Python shell. You should carefully consider how to use inheritance to reduce the amount of duplicated code. You should also ensure that public attributes for a tree's symbol or children can only be set once, during initialization. After that, they should be read-only.Your class(es) should be declared in a _le called regex design.py.” (quoted from the handout).
I have say it was really hard to start this step, I think maybe because it is the first time I was not give any file and just implements functions/methods, I have to actually design the structure of this, even though this does not seem hard at all. I also got confused with part 2, so I kept doing part 2’s job (eg. translate input into a tree and etc.); I over-complicated thing a bit.
I started by just trying out different things and design the classes in different ways, yet the far I go, the far I got lost (still confused the different parts I need to do for part 1 and part 2). So, I deleted everything and decided to start fresh. I have a friend in UT Scarborough, who is taking 148, too, and I ask him about how to start this, and I also went on to Piazza and spend three hours looked through all the posts and answers about Part one. Finally, after five hours, I finally knew what I needed to do. Ironically, it only took me one hour the whole thing. :P And it was actually pretty simple. :D
I think the handout did not explain everything to well. For example, what do we suppose to return when we use __repr__(), what suppose to be the input, and small things that are similar to these kind of questions. So, I hope in the future, the handout could have been more clear about the steps and explanation of the everything.
By the way, I really LOVE Piazza!!!! Every time I got confused about something, like assignments, exercises or labs, I go on there, and ask or look over people’s post, and it is really helpful for my studying in this course.

Friday, March 14, 2014

Week 9 - Lab 8

This week is kind of busy for me…. T^T Yet, I have been quite enjoy to be busy. :D As long As there is no tests or exams. :D


Anyway, this week, nothing every important or special happened; yet, this week’s lab is kind of interesting. The handout asked us to write the function/method count_less(self, item) for three times. This function /method needs to return the number of nodes in this BST with items that are less than given item.
The first time, we need to write it as a recursive method in the file BST rec1.py within the classBST(object), and we need write a nested helper function within count_less(self, item) without changing or adding anything to the class _BSTNode.
The second time, we still need to write it as a method in the file BST rec2.py within the classBST(object), but we need to write the helper method in class _BSTNode and calling the helper within count_less(self, item) and without changing or adding anything to the class BST.
Finally, the last time, we will implement method count_less(self, item) in class BST, yet we also need to write a helper method in both class _BSTNode and _BSTNone and calling the helper within count_less(self, item) without changing or adding anything to the class BST.
My partner and I were confused at first, wondering why we need to do the same thing again and again in three difference ways. Things turned out to be really interesting: the first time is just a bit challenging for us to come up with the solution, the second way is just want us to learn the way to import a helper method within another class. And the last method is trying to help us develop another way of thinking, divide the method into two situation: node is None or node is actually another parent or a leaf, and call the different helper methods depend of the node.
By the end, I felt pretty beneficial from this lab, if I were not had this lab, I would just put everything in one class, yet I now kind of really understand why we put different things in different classes and inherit from each other. :D


Sunday, March 9, 2014

Week 8 - Exercise 3

This week was horrible for me…. :(
I got sick with cold and fever on Tuesday night and I have to miss Wednesday’s lab since I was feeling really awful.
However, I am glad that I am finally feeling much better today (Sunday). :D I think I am gonna be back to normal next week sometimes.
I looked over Exercise 3 during the weekend and finished a draft for it. This exercise is about TreeList, yet it is different than the example the professors did in class: translate a TreeList to a list of the nodes; of course, we have choices between preoder, inorder and postorder. Yet, in this exercise, we suppose to do the opposite: by providing the preorder and inorder node lists, we convert them back to a single TreeList.  Now, I am just waiting nervously for the autograding result. :P
I always love the exercises better than the assignments, because they are much more “friendly”.  :D   Assignments just always make me nervous and feel like I am going to fail the course. I sincerely hope the assignments in the future courses can be “friendlier” and less stressful for us. My group finished Assignment 2 Part 1 already, yet just by looking at Part 2, I already feel kind of stress.  :(   


Sunday, March 2, 2014

Week 7 - Recursion

After an awesome week of reading break, week 7 was totally stressful and tense. I had 4 tests in 3 days in a roll…… I would never want that to happen ever again…… :(  Luckily, that was all finished.
Since week 4 ish, the professor already started to discuss about recursion with us in lectures. Recursion was something new to all of us; to think of writing codes that would use itself as part of the codes over and over again was novelty and cool to me at first.
According to http://dictionary.reference.com, recursion is defined as:
“the process of defining a function or calculating a number by the repeated application of an algorithm.”
In python, as far as we have learnt, we have seen functions which they call other functions in the same (or imported) files. Recursions is basic to call python’s ability for a function to call itself. 


It was kind of hard to imagine what it is when the professor just introduced this topic, yet he showed us a function called rec_max: it find the maximum number in the nested list from the input. This is a pretty typical example for recursion. The function would look through all the nested lists within the input to make sure that the number it returns would be the biggest of all:    
Another thing that the prof brought up a very interesting function, “Turtle”. Sorry that I could not find a correct image for the visualization of “Turtle”. Yet I found a similar one to demonstrate what does the code do:
We first set the speed and the color of the pathway of the turtle when we initialize it from class Turtle. Then, we input how many levels (recursions) and base (length of the pathway). Basically, if level equals to 1, the turtle would just draw 3 lines in 3 directions (120o apart from each other) with 3 different colors of the base’s length. If the equals to 2, the turtle would add another half of the lengths and draw 3 more branches with 3 lines the turtle drew when the level equals to 1. I made some pictures from level 1 to level 4:

       

   

As one can see, as the input of the level gets larger, the resulting picture will slowly form a huge triangle.
          In sum, recursion is just a smart trick for the programmers to use when they have to deal with nested things (lists, tuples etc.) or situations that would involve in break it down to tons of smaller pieces.

Thursday, February 20, 2014

Week 6 - Some Fantastic Blogs that I Read


            During the weekend, since the reading week is coming up, I had some free time that I can browse some of my classmates’ Slogs. It surprised me that some of them even have pictures and many paragraphs to go with it. For instance, these blog sites:


            → http://cscslogging.wordpress.com/

            This person put down ideas of his/her own, and yet also reading some web pages to research for his/her blog. Therefore, he/she provided some links that he/she had referenced. This is really formal in the way of reading, and he/she is really aware about plagiarism and respect other people’s work as well.


            → http://csc148courslog.blogspot.ca/
This is also a very cool blog. This person utilized tons of pictures so that the reader would not get bored while reading the entries. He/she also put in a cool GIF stool picture in one of the entries,

And also some interesting photos illustrating recursions.
One of the favourite is his/her last sentence for the first entry, which was about the object-oriented programming. When he/she was talking about we were going to learn recursions, he/she put down this.. :D 


            → http://hepingsheng.wordpress.com/
This blog is one of my favourites! He/she included lots of colorful picture and quotes from link. Sometimes, one can even find some useful link he/she suggested to watch about some certain topics. The links re in brackets right after the quotes. He/she made them so stand out to illustrate them as quotes.

From the contents and the lengths of it, one can see that this person did not do his/her entries in rush or just want to finish them as soon as possible; he/she did them really detailed and with his/her person ideas and thoughts. I enjoyed really much reading his/her writing. Also, he/she included a picture to show the method about stools in Assignment 1, the picture looks really nice and neat. :D 

He/she did put a lot of thoughts when he/she was creating his/her entries.
I had a really great time seeing other students’ blogs; it would also be a pleasure to have my classmates to leave comments for me as well. :D I sincerely hope someone would read my blog and give me some feedback.


Sunday, February 9, 2014

Week 5 - Inspired Week

Since last week, we have been learning/doing codes about recursions, yet recursions will be the writing topic for week 7. So, I would like to write about the class in general. :D
I personally was not used to the professor’s teaching style when the semester just started, maybe because 108 was very slow speed and its professors would ask if we had any questions every 10 minutes-ish. Yet, 148’s professor started the lectures very fast and purely focused on the lecture materials. Now, I have to say, I like his style better. :D In his class, I actually pay more attention to the material, keen to learn more and I can even gain some new details about the things I learned before. Even though the prof looked kind of serious sometimes, but I can feel that he is passionate and excited about the contents he is teaching. He always ask students’ names after they answer/ask questions, and he will remember those names. None of my other courses’ professors have done that before.
I feel really lucky to have him as my 148 professor, he inspired me to take more interest in python, and computer science in general. Also, some example functions he used in class were very cool. :D For instance, this week’s turtle recursion code is really fun to play with, and learn to predict its results from different input was really difficult at first. After the input 3, I started to get a handle of it; I can see the results will slowly form a complete triangle. Learning tracing codes with him was a very fun experience, and he is always patient to take the time with us with varies of inputs.
Unfortunately, Assignment 1 will be due this Thursday, so much pressure. L Luckily, the reading week is coming, I can finally take a little break between weeks of learning. 

Monday, February 3, 2014

Week 4 - Exceptions

This week, we started with something that we have never learnt before: Exception. Exceptions should be class objects. The exceptions are defined in the module Exception; it is a built-in class and I can always build new exception as subclasses of it and the python will automatically know it will deal with exceptions. The following Exception can only used as a superclass classes for other exceptions. A typical look of Exception will be this:
class E1(Exception):
       """
       An exception class that is a subclass of Exception.
       """
       pass

class E2 (E1):
       """
       An exception class that is a subclass of E1.
       """   
       pass

def exception_raise_function(x) -> None:

       try:   

        if something:
               raise E1

        else:
               raise E2('hey, I’m E2')
       
        except E1:
               # can print/return/etc. things

        except E2:
               # can print/return/etc. things
In class Exception, a try statement with an except clause that mentions a particular Exception subclass, that clause also handles any exception classes derived from that class. A exception message that coded within the bracket in the raise statement can also be printed when this exception is been raised. A test class could be designed to test the exception code:
def test_exception_raise_function(f, x) -> None:
       """
       f is the name of the exception function, x is the input of the                               functions.
       """

    try:
           if f(x) is None:
                  # can print/return/etc. things
       
    except E1:
           # can print/return/etc. things
   
    except E2:
           # can print/return/etc. things
The raise of a exception can sometimes return something long and messy. Yet, by using designed testing function, one can see whether the exception function was correctly coded by the things it returned.
Python raises exception in case of errors, and I can design codes to deal with these errors. I can use the try statements and except statements to raise exceptions when an error is produced. All exceptions are designed/subclassed from the built-in superclass, Exception class. I am still a little bit confuse about the differences of put try and except statements into different orders or levels of indented if statements, yet I think I have already got some grasp about exception; I just need a bit more practice :D.