Tuesday, 31 March 2015

CSC148 - Entry 9

Entry no. 9, Week 11:

Topics

1. Revisiting an old post.

Another short blog entry (Sorry, not much to write anymore).

I looked at my 2nd entry and some subsequent posts, and can definitely say that the pacing of the lectures and overall difficulty of the material grew from then. Even though I've encountered the material thus far, applying in the ways that we did was a nice experience. Regarding the beginning of the course, I'm an odd example as I was less impacted by the scaling into Assignment #1 than one of my collegues. I agree that Danny is an excellent professor, as well as the labs being a good basis for encountering new code, but the assignment wasn't too hard for me to manage solo.

Thursday, 26 March 2015

CSC148 - Entry 8

Entry no. 8, Week 10:

Topics

1. Thoughts on Week 9

This weeks topic is Week 8; Term Test #2

Short blog entry because this was a very uneventful week

Term Test #2 was this week, and it covered topics from Weeks 5-8. The material on it included Trees, Methods of Traversal, Recursion on Binary Trees and Iteration on Linked Lists. I felt like I did pretty well on this test by because this material isn't too new to me. Compared to the last Term Test, it required more knowledge of programming but was still at a level where the average undergraduate would do well if they completed their labs. That being said, thanks to the strike the past few tutorials were cancelled along with the absence of our TA's. As much as not having to go to tutorials is nice, I am concerned for some of the other undergrads in this course who are in need of a TA.

Tuesday, 17 March 2015

CSC148 - Entry 7

Entry no. 7, Week 9:

Topics

1. Impression on Week 8

 

This weeks topic is Week 8; Linked Lists.

An easy way to describe a linked list is as a tree with an arity (branching factor) of 1 which means there is only 1 'child' for each node.
This also means that this linked list will be linear and traversing can be accomplished by iteration where one can loop through the list until the end or until an object is found.
Example of a linked list

Generally for a linked list a class is used to keep track of it's information, a Wrapper class. Typically, it stores the size, front and end of the linked list but can keep track of anything within the linked list. 

Example of a wrapper class 

With further implementation of __str__, __repr__, __eq__ and other methods, a linked list can be extremely useful.

Something to be weary of when using iteration on a linked list is mutation. If one isn't careful, objects inside the linked list may be changed or swapped with each other.

Monday, 9 March 2015

CSC148 - Entry 6

Entry no. 6, Week 8:

Topics

1. Impression on Week 7


This weeks topic is on Week 7; Binary Trees (BT), Traversal and Binary Search Trees(BST).

In my previous post I used a Binary Tree to help illustrate a use of Recursion.
For this entry, I'll be writing more in depth on BT's, Traversal and BST's.

The weeks material is something I've learned before, so it's nothing new. As a result I feel like I already have a firm grasp on it.

BT's are variations of Trees in the sense that they are limited to 2 predecessors, Left and Right. We call their predecessors children and can each represent another BT. An empty sub tree can be identified as a NoneType because it is in the set of everything.

Traversal is the ways that we visit each node in a BT. The three simple traversals are Pre-Order, In-Order and Post-Order, and will each go through the tree in different ways. These are recursive definitions that were defined in class.
  • Pre-Order:  
    • Visit the current node
    • Traverse the left sub tree Pre-Order
    • Traverse the right sub tree Pre-Order
  • In-Order:  
    • Traverse the left sub tree In-Order
    • Visit the current node
    • Traverse the right sub tree In-Order
  • Post-Order:
    • Traverse the left sub tree Post-Order
    • Traverse the right sub tree Post-Order
    • Visit the current node
BST's are BT's that store comparable values and separate their left sub tree with data that values less than itself and a right tree with data that values greater than itself. We assume that if a value is added to a BST that already contains that value, it is not added.

BST's are useful because they cut down the search time to Big-O(log2(n)) where n represents the amount of data stored within the tree. Using the properties of a BST, we can eliminate an entire half of the BST and check the other half for the value. However, if a BST is not balanced (ie. all the data is stored within the either only the right or only the left sub trees) the search time grows to Big-O(n).

Sunday, 1 March 2015

CSC148 - Entry 5

Entry no. 5, Week 7: 

Topics

1. Summary of Recursion


This weeks topic is on Recursion.

In general, recursion is an extremely powerful technique where a function calls itself on a smaller instance of the problem to simplify the initial problem. For coding, recursion is beneficial but has certain drawbacks. Recursion allows us to create algorithms that traverse trees easier. On the other hand, a weakness about recursion is that it takes up lots of memory in stacks and is slower to compute for larger sets of problems.

Let's take a look at some sample code from a lecture exercise

Getting started on coding evaluate, we must first define a base case. The base case is a case simple enough that it requires no recursion. In this example, when the node is a leaf (ie. no children) it returns the float value inside. 
Eg.
if b.left == None and b.right == None: #No children
    return b.data

Moving on to the recurse part of evaluate the recursive case when the node is an interior node (ie. has two children) will be looked at, we can use the built-in python eval method to evaluate a string that involves arithmetic.

Using this binary tree as an example, we would have to call evaluate on the left and right children, and append their values as strings to the current data in the interior node ('+') then call eval on that resulting string.




The body of the function would look something like this.
Eg. 
if b.left == None and b.right == None: #No children, base case
    return b.data
else: #Has children, recursive case
    return eval(str(evaluate(b.left)) + b.data + str(evaluate(b.right)))

Since the recursive and base case both returns a float, the type contract is satisfied. When the method is called on a larger binary tree, it will traverse the tree in such a way that the values from smaller binary trees will be returned and then calculated. Thus since this tree works for smaller cases of binary trees, it will work when called for any binary tree provided that it is not too big for the stacks to handle.

Recursion is an important aspect of computer science because it makes solves problems by breaking them down into sub-problems, like what we did above.