The 2016 winter term is drawing to a close, and there are a couple comments I’d like to add onto my week 6 review. Almost everything in the original post still holds, and I primarily want to comment on a couple things that I couldn’t comment on at that point in the course.
First, the assignments and tutorials have been hugely useful. It’s probably just my learning style, but the course notes and lectures - both mostly focused on theory - where not nearly as effective as hands-on work. For learning terminology and the basic concepts they were required, sure, but beyond that starting point they didn’t really help me understand what was going on in the course. That may have been because I was already familiar with much of the theory, but nonetheless both the assignments and tutorials were what caused the majority of my growth in this subject. The tutorials consist of good, bite-sized questions that are effective warm-ups. The assignments on the other hand really helped me work more efficiently with other people. Together I think they are what I will remember after this course.
Second, handwriting code is still annoying and can have silly consequences (see term test 1), but memorizing syntax has made coding a bit quicker and debugging much faster. Obviously code on paper is done due to the issues technology pose in a testing environment, but it’s not the worst solution by far.
Lastly, CSC148 hasn’t been as radical a change from CSC108 as I had imagined it would be. The focus has changed drastically from syntax to theory, but I haven’t really felt bombarded by data structures and algorithms (more of a light pelting, really). I suspect things will pick up a lot next year to make up for this, but for now I’ll just keep up with 148’s pace.
I think that just about sums up what I wanted to say, so till next time.
Thursday, 31 March 2016
Friday, 25 March 2016
Week 8 & 9: Binary Trees
Over the past two weeks CSC148 has focused on binary trees. Binary trees are a type of tree that have an arity of 2. This means that tree traversals can be reduced to a single if operation at each inner node. While there are certainly a couple places you might want to use a binary tree, the most obvious and interesting place is for storing data that has a useful inequality operation, like an int (7 < 8) or a str (‘abc’ < ‘dz’). Why? Because when set up correctly in a binary search tree, searching for the data in the tree is much faster than other data structures. To understand, we simply have to look at how we’d search for an object in other collection types.
In a list, set, or dictionary (if we’re looking for a value, not a key), the simplest way is to check every object until we find a match. This has a worst case where we’ll have to make a comparison for every single object in the collection. This is linear time and not efficient.
With a randomly assembled binary tree, the simplest way to search for an object is the same, but if the tree is created in such a way that a node’s left child has a lesser value and it’s right child has a greater value, then a comparison only has to be made for every node from the root to the desired value. Of course, if we’re searching for the least object in a set, and the root is the greatest object, then every node will be checked. However, if the tree is balanced – has the least possible height – then the worst-case scenario is one where only log base 2 nodes are checked. This is logarithmic time and is much more efficient.
A similar search can be done on a sorted list as well, which is partially why finding an efficient sorting algorithm is important.
In a list, set, or dictionary (if we’re looking for a value, not a key), the simplest way is to check every object until we find a match. This has a worst case where we’ll have to make a comparison for every single object in the collection. This is linear time and not efficient.
With a randomly assembled binary tree, the simplest way to search for an object is the same, but if the tree is created in such a way that a node’s left child has a lesser value and it’s right child has a greater value, then a comparison only has to be made for every node from the root to the desired value. Of course, if we’re searching for the least object in a set, and the root is the greatest object, then every node will be checked. However, if the tree is balanced – has the least possible height – then the worst-case scenario is one where only log base 2 nodes are checked. This is logarithmic time and is much more efficient.
A similar search can be done on a sorted list as well, which is partially why finding an efficient sorting algorithm is important.
Friday, 4 March 2016
Week 7: Recursion
Last week we also started looking at recursion, so here’s my breakdown of the topic:
Recursion
Recursion is when a function calls itself. What does that mean? It means that when the function is defined, the function itself is in that definition. In programming this means that in the body of a function, a call is made to that function. For instance, if you defined a function f that summed up the numbers 1 to n where n is an argument of f, then you could implement this in python through a loop or through recursion. Lets look at each of these cases in python.
Loop
f(n):
sum = 0
for i in range(n + 1):
sum += i
# equivalent to sum(range(n))
return sum
Let’s trace the function in the case n = 3
sum = 0
sum += 1 -> sum = 0 + 1 = 1
sum += 2 -> sum = 1 + 2 = 3
sum += 3 -> sum = 3 + 3 = 6
Therefore f(3) = 6
Now let’s look at the recursive implementation of this function:
Recursion
f(n):
if(n > 0)
return(n + f(n - 1))
else:
return(n)
Once again, let’s trace the function in the case n = 3
f(3): 3 > 0 -> 3 + f(2)
f(2): 2 > 0 -> 2 + f(1)
f(1): 1 > 0 -> 1 + f(0)
f(0): 0 = 0 -> 0
Therefore f(3) = 3 + (2 + (1 + 0))) = 6
As you can see, they do the same thing! As a general rule anything that can be done through recursion can be done with a loop as well.
Let’s break down what a recursive function has:
Recursive Case: Obviously, a recursive function has a recursive call to itself. This can’t be the only part of a recursive function though! Most recursive functions will call themselves forever until python gives up. For this reason we need a base case, where the function is not called again.
Base Case: This case is essentially the stopping point of the function. In the above example the base case very clearly defined by the if; else statements, where the n > 0 is the recursive case, and the else is the base case.
These two building blocks can be played around with and changed (multiple of each, etc.), but without both of them a function isn’t recursive.
At this point the most prominent question is probably "Why would I ever use recursion if a loop can accomplish the same thing?"
The answer is that sometimes it’s way easier to use loops and sometimes is way easier to use recursion. The above example is harder to understand using recursion than a loop, and is just as long, so it’s a case where a loop is superior. But say you were give a graph and two nodes, and were asked whether their was a path between those node. It is theoretically possible to come up with a function that uses while loops to answer the question, but it would be very difficult to understand. With a recursive function the process becomes much easier: just check whether the current is the final node and if not, then call the function on all the nodes the current node leads to.
Recursion is a very powerful tool, and it's fun to play with, so I'm glad were finally learning it!
Tuesday, 1 March 2016
Week 6: Review
Last week we finished our first assignment and
got back our midterms for csc148. Now that a couple weeks have passed I've
gotten more acclimated to the style of 148, and feel like I can comment more on
it. First however, let’s talk a bit about the midterm.
We have gotten our
midterms back, and they went about as I expected: most students were rushed,
and made silly mistakes. What’s interesting isn’t what happened on the midterm
for most student, but what didn’t happen for ~180 of us, namely the midterm
itself. During one of the midterm sections the fire alarm was falsely pulled. I
know that in final exams managed by the university there are often extra rooms
kept free in other buildings for such and event, but this wasn’t the case for
the midterm. Personally, I feel like cancelling a midterm for your peers is a
bit psychopathic, as your putting your own needs in front of many of your
peers, but what’s done is done. Now, onto my review of csc148 so far:
Review
The largest difference between this course and
my high school background has been the insistence on consistent, readable
docstrings, or API. In my previous CS education descriptions of functions and
classes have almost always been required but fairly pointless, given the
complexity of the programs I was creating. I always was creating a program that
only used a handful of classes written by me, and most of the time only needed
to be read by me. Now that I’ve done csc148 assignment #1 with a group of
people, the value of a good API has become apparent. Other facets of the
course, such as structure or subject matter, have not had the same impact on
me, largely because this course has been mostly about class design and
documentation so far.
Overall, I’d say that
csc148 is a logical continuation of csc108, describing more of the basic units
of programming. Unfortunately, my favourite part of CS is understanding and
creating algorithms to solve specific problems. Fortunately, we have just begun
looking at some algorithmic techniques.
We have started on recursion, but I’ll describe that in my next
post.
Subscribe to:
Posts (Atom)