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.
Lazy Logic - CSC148 sLog
Theory, implementations, opinions.
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.
Tuesday, 16 February 2016
Week 4 & 5: Stacks, Queues, and Linked Lists
Over the past 2 weeks we have been looking at a couple of abstract data types (ADTs) designed to store other objects. While I have used stacks, queues, and linked lists in the past, actually implementing them was definitely a learning experience.
Definitions: Sacks, Stacks and Queues
A sack is an unordered collection of objects. Objects can go in and a random object can be pulled out. They are similar to sets, but they can have multiple of the same object stored in them.
Stacks and queues are ordered collections of objects. Both are can remove and object from the end of the collections, but queues can only add objects to the beginning, and stacks can only add objects to the end. This means that queues are first-in-first-out, whereas stacks are first-in-last-out.
The closest python comes is the list class for stacks, and the deque class for queues, but neither of these are exactly stacks or queues. The list class does have an append (push) and pop function, which are all you really need for a stack, but it also has all sorts of other methods and properties which are not needed. Unlike private variables and functions that have a standard to protect them, there doesn't appear to be a standard that protects a stack implemented through a list from being misused.
The case is somewhat similar with deques and queues. "Deque" stand for double-ended queue: a queue that can push/pop an element both to/from the "left" and "right" sides of the queue. In a practical situation it would usually be reasonable to use this instead of creating a new queue class.
In class we inherited from the container class to implement these ADTs, and used them to solve simple problems like parenthesis counting. Surprisingly, this wasn't a purely theoretical exercise to teach us about the structure of stacks and queues, as python doesn't actually have these as a module/class.
Linked Lists
Linked lists are more different than lists than the ADTs described above, and on a lower level. In lists each object is stored after the previous element, which makes accessing a particular index quite fast, but makes inserting an object quite slow, as all the objects behind it have to be shifted as well. To counter this a linked list can be used. A linked list is made up of nodes, each which has a pointer to a value stored somewhere in the memory allocated to the program, and a pointer to the next node. This way, getting to a specific index in the linked list takes time as each node before that node is traversed, but inserting an object is as simple as creating node that points to that object and the next node, and changing the previous node's pointer to point at the new node.
Obviously, linked lists are better than lists if the list is quite long/has large objects and a lot of inserting/removing is done. Linked lists also have issues with memory efficiency, as they by nature require nodes, which are more memory intensive than lists. In most situations I suspect that using lists or other ADTs would be better, but having experience implementing a linked list is better than not having experience implementing one.
As an aside, apparently python automatically cleans up objects that are impossible to access anymore. I guess I already knew this considering that variables are just pointers to an object, and variables change. I was still surprised however by node removal in linked lists simply requiring a change of pointers, with no code to clean up the node itself.
As an aside, apparently python automatically cleans up objects that are impossible to access anymore. I guess I already knew this considering that variables are just pointers to an object, and variables change. I was still surprised however by node removal in linked lists simply requiring a change of pointers, with no code to clean up the node itself.
Midterm
Last week we had our midterm for CSC148. Before the test, I really wasn't sure what was going to be on it. We haven't covered that much so far in terms of algorithms, and the previous midterms had different material on them so they weren't particularly useful. So I memorized the formatting we needed, syntax of properties, inheritance, etc., but still wasn't feeling too certain entering the exam room: had I missed some critical example or lecture, what on Earth was going to be on the midterm? As it turned out, none of the questions were particularly hard: anyone who had attended lecture and tutorials as well as made sure they understood them should have been able to answer them, given enough time. Unfortunately, the 50 minutes given to write the test were - for the majority of people I have talked to - not enough time.
Given the great importance of marks in this course for anyone who wants to get into the computer science POSt, this test assumedly reflects what is needed. Therefore it's interesting and useful to see that speed, exact knowledge, and decisiveness are the traits that ensure success in later years.
This week is reading week, so stand by for a post about an interesting cs problem. Have a nice reading week, whether you're relaxing or trying to stay afloat.
Wednesday, 27 January 2016
Week 3 - It Begins
Weeks 1-3
So far we have started with object-oriented programming: objects, encapsulation, inheritance and composition. Coming from a high school background in Java, these concepts are familiar, but there is some "culture shock" looking at how they are implemented in python, specifically encapsulation. Java is very strict about which parts of the program can see what, whereas python has a more lassez faire approach: private variables are not hidden, the client is simply expected not to directly modify them. This was mildly disturbing at first, but then I realized that it was essentially the same thing in practise (and apparently private variables in java aren't so private anyway). I still feel that on large, messy, built-up projects it could cause some frustrating debugging, but I suppose that just reinforces the need to always have good style. Once I get used to the changes, programming in python will probably be much more fluid than in java, and little shortcuts like properties and list comprehensions bolster that.
Practical knowledge that allows us to implement OOP and ADTs is useful, but my interests are in the theory behind them as well as the algorithms that we'll be studying later in the course, so my future posts will be more detailed.
Subscribe to:
Posts (Atom)