Saturday, 4 April 2015

Week 12



It is with great regret that this is the last weekly slog.

Yes, the term has been completed, and now exams are upon us.  Before we conclude, here are my last thoughts on CSC148H1 S Introduction to Computer Science 2015 with Danny Heap and Diane Horton (who, by the way, were fantastic professors :)).

I came into the course not really knowing what to expect based on csc108.  After taking this course, I can say that I greatly enjoyed this course.  Although the thought process and general approach to the class was different from that of csc108 and 165, I think I learned just as much, if not more.  In 148, in my opinion, there is a bigger emphasis upon connecting or piecing ideas together ourselves.  In 108, instruction was very straightforward.  In 148, we were given more conceptual ideas, and explored them through the labs and assignments.  I do not know what lies ahead in csc207, but I hope I will be able to say that 148 prepared me well.  Here are some of the most important learning tools we had this year.

ASSIGNMENTS
Although we struggled with assignment 2, the assignments were probably the second most helpful tool we had for learning.  We were able to do pretty well on the first assignment by simply following the handout and posts on piazza.  However, we struggled on assignment 2.
LABS
The Ta strike hit everyone pretty hard.  Unfortunately due to the strike, we were not able to practice the labs with he help of our ta's.  Fortunately we were still given the labs, but we were unable to interact with our TA's.  I believe that everyones' learning experience was severely set back by the strike.  In my opinion, labs are the most important learning tools in the course.  Being able to practice skills learned in class in a limited period of time (unlike the assignments) is a good preparation for the test and a (hopefully) accurate evaluation of your comfort with the course content.
Last but most certainly not least.... SLOGS!!!
I personally dont think that slogs have as much value as labs do, but I can't say that slogs are worthless either.  I gave my impression of my slog experience last week.

Week 11 (Looking Back)

Last week in class we continued discussion on the second assignment and possible solutions.  We had trouble with minimax, and tried implementing minimax based on the suggestions in dannys email.
We also ran into another problem we discussed in class.  Instead of mutating the game state, we found out that we had to make copies of the gamestate instead.


On the topic of revisiting old slogs:
I don't think my opinion has changed much over the course of this term.  I still think slogs are useful for looking back and reviewing past material.  I can look back to previous slogs when I'm studying to recall anything important danny said or any code that i thought was important to remember for the future.  In my week five slog I mentioned that I felt something was missing.  In retrospect, I could have been more active on my slog.  I only really wrote on my slog once a week, as assigned, when I should have wrote more often, whenever I was having trouble with labs or the assignments.
My favorite slog is also my longest one, from week six.  I probably put the most effort into this weeks slog; hopefully I will find the time to go back to some old slogs and elaborate on topics that I have learned more about, or just anything that I think i missed.  thROUghout the term I have tried to read some of my colleagues' slogs as well.  Here are two that I read:
http://csc148sonny.blogspot.ca/
http://rockycsc148slog.blogspot.ca/
Both slogs have good summaries of general concepts and specific ideas mentioned in class.  I think that I should have read more of my peers slogs more often.  This would help me write better slogs too.

Week 10 (week nine impressions)

Last week we had our assignment and this week we had our test.  I really with the strike was over so I could have talked to my TA and asked him for help on the assignment.  In class, we continued studying BSTNodes.  The insert function we looked at in lecture was similar to previous functions we have seen before.

def insert(node, data):
    ’’’ (BTNode, object) -> BTNode
    Insert data starting at node, and return root.
    ’’’
    return_node = node
    if not node:
        return_node = BTNode(data)
    elif data < node.data:
        node.left = insert(node.left, data)
    elif data > node.data:
        node.right = insert(node.right, data)
    else: # nothing to do
        pass
    return return_node

This code looks similar to that of the bst_contains function (mentioned in week 8), as it checks whether data is greater than or less than the node before recursively calling itself on the left or right node.

We also briefly covered the assert statement, which basically just raises an error if and expression is False.  We didnt study that much else in class this week because we had the test.  The lab for the week covered inserting objects (e.g. values and strings) into a btnode and a delete function different from the one in class, balancing the deletions on each side of the tree.


I felt pretty confident about the test we had last week.  The questions were similar to what was in the labs, nothing very surprising.

Sunday, 15 March 2015

Week 9 (Week 8 Impressions)

The TA strike is making matters difficult for everybody.. I hope it is resolved quickly.

Last week we started learning about linked lists.  Linked lists are very helpful when you want to insert an item to a list because linked lists allocate memory more efficiently.  However, to find an item in a linked list, you must walk through the list to find the item.

In a linked list, each object or node has a value and a reference (or link) to an object that comes after it:
    def __init__(self, value, nxt=None):
        ''' (LLNode, object, LLNode) -> NoneType

        Create LLNode (self) with data value and successor nxt.
        '''
        self.value, self.nxt = value, nxt

You can also think of linked lists where the first node is the first item in the list and the second item is a sublist of comprising the rest of the linked list.
We say that we use a "wrapper" class to store the front, back and size of the linked list.

The general approach to walking through the linked list (as aforementioned) is as follows: you "pick" a node, and use a while loop to set the current node as the next node until the condition is False.


I did have some trouble with the lab but it I understand it much better after reading the solutions.  I was thinking of more complicated solutions for append, prepend, and delete_front.  I tried implementing them without using front, back, and size.

Week 8 (Week 7 Impressions)

Last week we continued our work on assignment 2 and continued learning more about trees.  Instead of our previous tree examples with self.children, we focused on a special kind of tree, binary tree nodes (BTNode).  BTNodes still have __repr__, __str__, and __eq__ methods as usual but instead of having self.children, these nodes only have (at most) two children: self.left and self.right.  For binary trees, data must be comparable, and all data in self.left must be less than node's data and all data in self.right must be greater than node's data.  Binary trees are used because they are efficient when searching along a single path.  These trees can also be used to represent arithmetic expressions, i.e ((3*4)-7)/2.  This would have the root node = '/', self.left = -, self.left.left = *, self.left.left.left = 3, self.left.left.right = 4, self.left.right = 7, and self.right = 2.  To evaluate such a tree, return self.data if the tree is just a leaf, or evaluate the left side and right side separately using python's eval and combine left and right.

Here you can see two important methods for binary tree nodes.  The difference between the two is that bst_contains checks whether the wanted value is greater or less than the node value, before calling itself on the node's left or right child.
def contains(node, value):
    ''' (BTNode, object) -> value

    Return whether tree rooted at node contains value.

    >>> contains(None, 5)
    False
    >>> contains(BTNode(5, BTNode(7), BTNode(9)), 7)
    True
    '''
    if node is None:
        return False
    else:
        return (node.data == value or 
                contains(node.left, value) or 
                contains(node.right, value))


def bst_contains(node, value):
    ''' (BTNode, object) -> value

    Return whether tree rooted at node contains value.

    Assume node is the root of a BST.

    >>> contains(None, 5)
    False
    >>> contains(BTNode(7, BTNode(5), BTNode(9)), 5)
    True
    '''
    assert node is None or isinstance(node, BTNode), (
        'Not a BTNode: {}'.format(node))
    if node is None:
        return False
    elif node.data > value:
        return bst_contains(node.left)
    elif node.data < value:
        return bst_contains(node.right)
    else:
        return True

We also looked at the recursive functions inorder, preorder, and postorder for visiting tree nodes.
Inorder visits the left tree, the node, then the right tree.  Preorder visits the node, the left tree, then the right tree, and postorder visits the left tree, the right tree, then the node.

Monday, 2 March 2015

Week Seven

For this week's slog's graded topic, I'd like to use my summary of OOP from week six. http://mattfcheung148.blogspot.ca/2015/02/week-6-oop.html

The rest of this week's slog will be some more reactions and some brief thoughts on trees and binary search trees:
I can already see many applications of using trees.  Without any prior experience using trees, they seem like a very intuitive method of storing data.  Binary search trees seem to have fewer applications, besides searching of course, but seem very efficient at doing so.

So I'm still getting used to writing weekly slogs.  In csc165, I didn't really write them every single week, so this I guess this is my first real weekly course blog.

Saturday, 28 February 2015

Week 6: OOP

I guess I forgot to upload my week six slog..

This weeks slog is on object oriented programming concepts in python.  Some of these concepts we used in tutorial and our assignment were: classes, class variables, instance variables, instances & instantiation, inheritance, methods, constructors, and objects.

A class is an abstract data-type, which contains information about the data stored in a class and the operations used on such data.  As we move away from functional programming towards object oriented program, we use classes to group together data that is related and operations (methods) that we use to manipulate or access the data.  Classes have instructions as to how the data is used and attributes the data takes.  Classes are used to create objects, and instantiate instances.  A class is like the definition, while an object is a type of data being represented and the instance is the specific object that has been created in memory.

When I mentioned data stored in a class, this refers to variables: class variables and instance variables.  The distinction between class variables and instance variables is that class variables are defined in the class but outside methods, while class variables are defined in methods themselves.  In practice, we identified variables by reading through a description of a class and choosing which attributes and actions would become the variables and methods of our class (e.g. on the example on the test with undergrad and grad students, their grades, classes, and graduation requirements).

Instances are created in instantiation.  There are multiple ways of doing this and a proper way using the __init__ constructor method.  It is possible to create an object and make a new instance with only three lines of code such as in the point example:
class Point:
   pass
p1 = Point()
Here we have a class, Point, and an instantiation of point p1. The proper way to instantiate a point required the implementation of __init__. The __init__ method is also often used to create class variables. Class Point has variable coord, class Stack has variable _data, and class Shape has variable position. Observe that in Stack, _data is different from Point and Shape because the underscore _data means that it is not accessible to the public user interface. This draws on the OOP concept of data encapsulation and data hiding. This serves to prevent conflicts between data attributes and method attributes. These special attributes cannot be changed except through special methods get() and set(). We briefly discussed this during week 3, but we have not had to use this in our assignment. We did, however, use inheritance, a concept related to class/instance variables but not quite the same.
In this course, inheritance has so far mainly been dealing with subclasses. Inheritance is basically how subclasses/child classes derive data and behavior from its super class/parent class. To inherit the attributes of a parent class, we would write, for example, Square(Shape). Subclass methods can also override methods of their parent class if the parent class method is implemented to raise an error stating that the method should be implemented in the subclass. We used this frequently in our first assignment and will be using it in our second. We could have coded subtract square without using subclasses (we could even have coded it without using object-oriented programming) but because we are reusing gamestate for our other games in assignments 2 and 3, we created gamestate as generally as possible so that it would work for any two player, zero-sum, full knowledge game.

There are some differences in methods in OOP and functions, some of which was covered already. They are fundamentally different in that methods are always in a class and defined by that class while functions are not. Methods can be written in the parent class or child class. Three other important special methods we have used frequently so far are the __str__, __repr__, and __eq__ methods. These are used to represent data in forms readable by users, for python, and for equating instances.