Monday, April 6, 2015

Final Thoughts

During the last few lectures, we talked about running time of code, worst cases and big-Oh. I already learned this during CSC165 last term, but it was still worthwhile to go through and review it again, as it was one of my weaker aspects during CSC165. Our last assignment was due on Friday, and I found it the easiest out of the 3 we had to do. I chose to do option A, because in the process of working on and testing the previous assignment (A2), I had been really bothered by how long the code took to execute in some cases, and had already begun thinking about ways that I could get the program to work more quickly, so being able to actually do it in assignment 3 was really satisfying. 

Classes have ended and now all that's left are exams. Looking back at everything that we've covered, I don't think I found anything particularly challenging that I could not handle during the course. But that isn't to say that I haven't learnt anything. I discovered the importance of writing, improved my communication skills by working with my lab partner, learned about recursion and various abstract data types, and picked up the skills of tracing and debugging code. Overall, the course has taught me quite a few things, and was an extremely valuable and enjoyable experience. I look forward to doing more computer science next year. 

Monday, March 30, 2015

Revisiting an earlier post

So after looking back at all of my previous posts, I figured that it would be most appropriate to revisit my very first post, Why Geeks Write. As I stated in that post, coming into CSC148, I saw SLOGs as a massive waste of time because doing it did not teach us any new computer science content. I personally enjoyed writing creatively in my own time, but I did not appreciate SLOGs because it felt 'work' and was too technical for my tastes. Surprisingly, I got really into writing that very first post and it ended up changing my perspective on writing about computer science to a more positive one. That post made a very big impact on me throughout the rest of the course. I thought about it while writing every single one of my other blog posts, and always put effort and genuine thoughts into my writing like I would with my creative writing, rather than trying to get it done as quickly as possible and making up things that I think the TA marking this wants to see in order to get marks.

When I wrote that first post, I had no actual experience, and could only see the theory as to why writing about these technical things would be useful. However, as the term progressed and I continued putting effort into my posts, I actually felt the benefits. Writing summaries of the topics we covered each week in lecture helped me gain a better idea of how well I knew the content, and actually helped me get to know the content better because I had to review and make sense of things in order to write comprehensible summaries in the first place. These summaries also acted as notes that I looked over as preparation for tests. As well as this, writing down my thoughts and opinions each week helped me reflect on things I was good at, and things I needed to work on. I sometimes even wrote down little things that I had to do (such as in my 3rd post, Tracing Recursion) and it served as a reminder to actually go do these things. Sometimes when you tell yourself you need to do something, you lack the motivation to actually do something, especially after you leave it for a period of time. But when I put it down in a post, and I know it's "out there" for other people to see, it feels more binding, and provided me with just enough of a boost to actually do the things. It was also just fun and pleasant in general to look back at what I wrote, remember what I felt back then, and see how much I have grown since then. I saw writing as an outlet to express myself and let my creativity flow, and thanks to that first blog post and this SLOG in general, writing now means even more to me.

I looked at some of my classmates' SLOGs, and was surprised to find that a lot of blogs were not being updated at all. It disappoints me a little seeing that not everybody sees the wonders of writing. However, I did find a few blogs that regularly updated and had some pretty good content. It was interesting to see what people found challenging or easy and how they perceived the content of the course. One particularly memorable thing I came across on a blog (http://juliansongcsc148slog.blogspot.ca/) said that computer science is so much more than just coding. This perfectly put into words what I had slowly manifesting the back of my mind as my studies progressed, and I completely agree with it. Computer science is so much more than just 'this code does this', 'typing this will do this'. Careful, logical consideration, planning, and even some amount of creativity is required in order to put together algorithms, the efficiency/running time of code also needs to be taken into account, there is math involved, and being able to discuss and communicate ideas to fellow computer scientists is also essential. There is so much more to computer science than I previously thought, and I look forward to discovering even more as I continue my studies.

Tuesday, March 17, 2015

Binary Search Trees

In lecture we went back to look at binary search trees, which we briefly looked at before when working with binary trees. A binary search tree is a binary tree where the value of the left child is less than the value of the parent node, and the value of the right child is greater than the value of the parent node.

When we worked on binary trees previously, the insert function to add nodes into the tree was always provided, and we never really talked about it. In lecture we finally got to take a closer look at this, as well as deleting nodes. Although there was no lab because of the strike, there was still a lab exercise, which provided the opportunity to experiment with, and further familiarize ourselves with the insert and delete functions.

I found the function delete a little tricky at first, but after playing around with it a little, I got it.

def delete(node, data):
    ''' (BTNode, data) -> BTNode
    Delete node with data if it exists and return the resulting tree.
    '''
    return_node = node
    if node is None:
        pass
    elif node.data > data:
        node.left = delete(node.left, data)
    elif node.data < data:
        node.right = delete(node.right, data)
    elif node.left is None:
        return_node = node.right
    elif node.right is None:
        return_node = node.left
    else:
        node.data = _find_max(node.left).data
        node.left = delete(node.left, node.data)
    return return_node

def _find_max(node: BTNode) -> BTNode:
    '''Find and return maximal node, assume node is not None'''
    return _find_max(node.right) if node.right else node

What this code does is basically removing a specific node from the tree if the tree contains the specific node, and then returns the resulting tree. If the node we are trying to remove is an internal node, we still want to keep it's children/the subtree under it, so we cannot simply locate the node and make it None. Instead, we replace the data of the node with that of its largest child in the left subtree, and then delete that child. This child is larger than all of the other nodes in the subtree, but is smaller than the node it is replacing. This way, the structure of a binary search tree - left node data is smaller than its parent's and right node data is larger than its parent's. An alternative would be to find the smallest node in the right subtree to replace the specific node we are trying to remove.

We can do this by replacing the code under 'else:' with:

        node.data = _find_min(node.right).data
        node.right = delete(node.right, node.data)

and use a helper function _find_min instead of _find_max
def _find_min(node: BTNode) -> BTNode:
    '''Find and return minimal node, assume node is not None'''
    return _find_min(node.left) if node.left else node

We were also prompted to think about the efficiency of functions on a binary search tree. For a regular binary tree with nodes randomly placed, to find a certain node in the worst case, we would have to traverse through all of the tree's nodes. However, with how the nodes are organised in a binary search tree, the worst case requires time proportional to the height of the tree, which would be a lot less than a regular tree with nodes that had more than one child. Overall, I found binary search trees to be fairly fascinating to think about and really fun to play around with.

We had our second test during our Wednesday lecture time slot. The test was a lot easier than I thought it would be. I don't think there was anything in particular that stood out, it was basically types of things that we had already done in either lecture or labs. 

Sunday, March 8, 2015

Linked Lists

A linked list structure can be thought of as an item and a ordered sub-list. It can be implemented on python with the use of 2 classes. The first class represents a node, which can be thought of as an item in the list. This class keeps track of the node's own value which can be any object type, and also has a 'next' attribute that points to the next node. A second class stores information about the entire list. It keeps track of the very first node (front), the last node (back) and the number of nodes in the list. It can be visualized as shown below.


There are many methods or functions that can work with linked lists. It is possible to check whether the list contains a certain value, to retrieve a value with its index, add a node anywhere in the list, delete nodes anywhere in the list etc. The majority of these things require moving along the list and examining each item. This can be done by creating a current_node variable and setting it to the front node of the list, then moving current_node along the nodes in the list.

current_node = self.front
while <condition>:
    <action>
    current_node = current_node.nxt
*self refers to the class that keeps track of the entire list

I found the concept really easy to grasp, and it is relatively easy to work with linked lists as long as I draw out examples as I am writing methods/functions that work on linked lists. I didn't find linked lists to be unpleasant, but I didn't find it very interesting either, but I'm sure there will be more fun new things coming up for us to cover in the course. 

Sunday, March 1, 2015

Recursion

Recursion is a way of coding where a function calls itself in its body. It basically keeps solving smaller instances of the same problem until a final solution is reached. Recursion can be quite complicated and confusing to trace, especially if there are large amounts of information that the code needs to process and so it is extremely easy to make mistakes that lead to massive errors in coding. A good way to approach coding with recursion is to identify the base case scenario(s) that do not require recursion, code this first, and then move on to add in recursive calls. It is useful to keep in mind that if the base is never met in the calls, an infinite loop will occur. Recursion can allow code to do things that it wouldn't be able to do otherwise, but in many cases, it is possible to work without recursion. In these cases, recursion can still be beneficial because it can make the code a lot shorter and easier to read or follow.

I talked about object oriented programming (OOP) and abstract data types (ADTs) in last week's post, and mentioned tracing recursion the week before that. This week, we got to combine these things and work on binary trees. A binary tree is a series of nodes, where each node can have a maximum of 2 children. Expression trees and binary search trees are examples of binary trees. An expression tree is a tree where each internal node is an operation and each leaf is a number, and working through the tree produces a mathematical expression. A binary search tree is a tree where the value of the left child is less than the value of the parent node, and the value of the right child is greater than the value of the parent node. We worked on many functions that could be used on these binary trees, such as functions to add parentheses and create an expression out of an expression tree that can be evaluated, a function to evaluate an expression tree, a function that lists all the nodes with values in between two given values, functions to find the height, longest path, number of leaves etc. At first, I found these really hard to figure out, but as I did more, I realized that as long as I stick to writing down the code for base cases that do not require recursion first, and then work on the recursion code, it wasn't actually all that difficult. Working on these things also helped me become more familiar and comfortable working with ADTs.

Saturday, February 14, 2015

Object Oriented Programming/Abstract Data Types

Object oriented programming (OOP) is based on creating objects and writing methods to create data structures that can store and manipulate data. OOP allows the encapsulation of data, meaning it allows us to restrict the access to some of an object's components. Python provides the property mechanism, which re-directs access to an attribute. So for example, you could make it so an object's attributes cannot be changed once it has been set once.

An abstract data type (ADT) is a model of a data structure defined by the methods/operations that it can perform (the semantics) rather than code details of how they are implemented, so ADTs can be implemented in multiple programming languages. In lecture, we have explored 3 specific ADTs: stacks, queues and trees.

A stack stores items and has an operation to remove (pop) the top item, an operation to add (push) a new item to the top of the stack, and a way to tell if the stack is empty. It operates in a "first in, last out" fashion, and can be compared to how a stack of books or plates would work in real life.

A queue stores items and has an operation to remove (dequeue) the bottom item, an operation to add (enqueue) a new item to the top of the queue, and a way to tell if the queue is empty. It operates in a "first in, first out" fashion, a lot like a real life queue of people lining up at a ticket booth of some sort.

A tree is a storage of objects on which a hierarchical ordering is placed. Each object in a tree is a node. A tree has many operations, such as access to the root node, determining the height of a tree, the depth of a node, the number of children etc.

When working with classes, sometimes a new class that adds a few features to an existing class is required. Inheritance can be used to create a subclass of the existing parent class that inherits all the methods and attributes of the parent class. This way we do not have to rewrite the code again, and can just focus on writing the additional features that we want.

I personally find OOP and ADTs to be pretty amazing because of how it allows countless possibilities. With OOP, you could create any kind of object you want to do whatever you want so that you are not just limited to default object types built into a programming language such as lists, strings or dictionaries in python. OOP has a clear structure, making it simple for you to code things, and easy for anybody else that will be using your code to understand what it does. You can easily modify or create similar objects. I have always had an interest in video games, and learning about ADTs made me look back on games I used to play, and recognize ADTs  - mainly the concepts of stacks and queues being used as inventory slots to store items that are used in the game. As well as this, our CSC148 assignment 2 asks us to implement a new strategy for games that looks at all the different possible moves and chooses the best one - the concept of trees. All of this is of great interest to me, and I am having a lot of fun in the course learning all these things and establishing a better understanding about how things that I found so awesome on the computer work.

Friday, February 6, 2015

Tracing Recursion (Week 4)

During week 4, we were introduced to recursion - functions that call themselves. I looked recursion a little during my spare time in the Christmas holiday before the term started, and found it quite confusing. However, the lab exercise provided an opportunity to look at and trace through many examples of recursive functions and even have a go at writing a recursive function, which allowed me to establish a far better understanding on the topic. I have no trouble tracing recursive code in my head now. However, I haven't exactly gotten the hang of writing out the all of the steps and function calls when tracing code. I thought that being able through it in my head and figure out the output of functions would be enough, but I realized I was mistaken when I got my lab quiz back and I had lost half a mark for not writing down the steps. I think this may have cost me some marks in test 1 as well.

I plan on looking back at the lab exercise examples to familiarize myself with the format for writing down the steps when tracing recursive code so that I will be prepared if questions like that were to come up again in the future. I haven't done this quite yet because I have more midterms coming up, but I will do so as soon as I find the time.