Thursday 28 November 2013

Sortof a Pun Related to Sorting

Selection Sort: This sort works by splitting the list into two parts: a sorted half and a non-sorted half. The algorithm then looks through the sorted half and puts the smallest element at the end of the sorted half.This occurs until the whole list is in the sorted half. Because the algorithm has to go through the whole list for every element of the list Selection Sort is O(n^2)

Merge Sort: Merge sort recursively separates the list into its smallest case (1 element) then takes two small lists and looks at the first elements. It then repetitively puts the smallest element in a new list until both the smallest lists are empty (or until one is empty and the rest of the other can go into the new list). The continues until all the 'mini-lists' are merged together into one big sorted one. Because Merge Sort divides the problem into smaller problems it has O(nlogn)

Quick sort: This chooses a point in the list then puts all smaller elements on one side and all the bigger elements on the other side. Now the point is at the right index. Quick sort then recursively Quick sorts the two sides. Quick sort also splits the problem into smaller problems and as such has a O(nlogn)


Monday 25 November 2013

Private and Classy

Private classes are now a thing in Python! Kinda. Not really but we at least now have a way to prevent people from easily tampering with our variables. They can still do I: just not easily. This is all thanks to the built in 'property' which takes 4 values. The first two are what you'd like to be the get and set methods for this variable. They can literally be anything (that compiles) but ideally they should return a copy of the requested variable and allow you to change said variable within the appropriate restrictions. The next parameter the property takes is delete. When attempting to delete this variable this is what is called. I wonder if by simply passing you could make your variable very hard to Delete. Lastly you can add read only docstring. At first I didn't see the point if your method already has docstring but then I realized that it might be useful to have docstring specific to the user to give him/her more relevant information ( or so that we're able to call the user funny names behind his back) 

Friday 8 November 2013

Test: Round Two

And so begins the second round of Tests. I think the things that killed me the first time all amounted to me not reading the question right so I going to make sure that I do that. A2 went well, I worked with a partner this time so it was nice to bonce off ideas with someone. A2 took a lot less time, partially I think because it was easier,  but also because I spent a lot more time considering the most basic cases before I wrote anything. More time thinking = Less time writing code = Less time doing homework overall.

One of the things on the test will be binary search trees. The most complicated thing to do with trees seems to be deleting nodes so Ill now explain that here:

- first check if this is a child of a leaf (ie None). In that case we didnt find the item we wanted to delete in this branch so return None. This is the simplest case
- Then check if the item to delete (ITD) is smaller then the current Node. If that is true the ITD is in the left subtree so look in there.
- Then check if the ITD is greater then the current Node. If that is true the ITD is in the right subtree so look in there.
- If the ITD is not smaller or greater and the Node is not None, that means we have found a match. And should remove it
- We can do this by first checking if either child is None, and if this is true then we can replace the node with the other child
- If neither child is None then we can either replace the node with the largest item from the left subtree or the smallest item from the right subtree. We then need to delete the original item we just moved.

Wednesday 30 October 2013

O sometimes, I get a good feeling

Today in class... We learned about big O notation! Yay! Conceptually it's a very easy concept. However I do find the grotesque oversimplification of information to be contrary to what I typically expect from this field of study.  If the Professors wished to establish propriety within big O notation I think a greater amount of stress should have been put on the fact that all extraneous constants are considered part of 'C'. This fact was initially unclear to the classmates around me and I as 'C' was given very little emphasis. 

Tuesday 15 October 2013

Comprehend List Comprehension

I just had my lab on List Comprehension and the TA helped me understand what I didnt even know was able to be understood. Namely I learned the now obvious notion that for loops dont just have to run through numbers, they can, in fact, run through many object types (but in this case I am concerned with lists). I learned that when you write:

for i in listExample

'i' doesnt represent the index, but rather the objects in listExample. I also made the descovery that you can have multiple 'i's. For example using tuple unpacking (assuming listExample contains tuples).

for i,n in listExample

My coding style, as a result, has shifted slightly and I can condense a method into fewer lines. For example, the code I put in last week's post, which I wanted to shorten, I have now shortened to one line

def list_count(o):
    return (1 + sum(list_count(i) for i in o) if isinstance(o, list) else 0) 

huzza!

Monday 14 October 2013

Middle of MidTerm

Oh happy day! Last week on Tuesday night I, Mackenzie Heller, against all odds, created a recursive method that worked! The solution? I deleted about half of my code. Turns out the 'solutions' I wrote for my bugs were in fact the problems themselves. I was unable to trust that if the program worked for the first case it could work for all cases thereafter. The program was handed in with one hour to spare. But now it's midterms, Ive been reviewing all the class notes and answering the preivous test we were given yet I still cant seem to get over the fact that I dont feel ready. I think thats because I havent really done any practice questions. A few questions on a practice test doesnt really seem like enough to me but maybe im just going overboard with how much i need to study for this (this being my first University test). In any case this is my answer for question 3...
def list_count(o):
    k = 0
    if isinstance(o, list):
        for i in range(0, len(o)):
            k += list_count(o[i])
        return 1 + k
    else:
        return 0
Im sure you could make it shorter but this is how it most logically makes sense to me.    

Monday 7 October 2013

Object-Oriented Programming's got class

Object-Oriented Programming is a form of programing that focuses around conceptual objects. Objects  posses properties that define what kind of object it is and methods that allow the object and its properties to interact with other objects. For example a 'ball' might have a 'shape', 'colour', 'size', and 'bounciness' while being about to do things like roll() and bounce(). Objects may consist of other objects and inherent traits of still more objects.  Because of its nature, Object-Oriented Programming tends to be modularized meaning larger goals that the program wishes to accomplish are split into smaller objects that interact with each other. Many  Object-Oriented Programming languages such as java protect its modules from being tampered by the outside. Java does this by declaring if an object is 'Public' or 'Private'. Private variables can often be accessed safely through Public methods conventionally named 'get' or 'set'. The program language this course uses, however, does not protect its modules. Instead Python asks nicely that you do not touch anything that's name consists of a '__' before and after. 
 
Object-Oriented Programming is useful because it allows you to use objects multiple times without having to recreate the code from them. By splitting problems into smaller problems, simpler and more effeicent methods can be found to solve said problems.