Sunday, 28 February 2016

Week 6 SLOG - Classes

It seems that I'm still being affected by reading week procrastination. In this SLOG, I'll be talking about Classes. I know that we were learning recursion in Week 6 but I'm saving that for Week 7's SLOG. This is because there are 3 topics we can do for Week 7, recursion, object-oriented programming, and ADTs. I've already done ADTs and object-oriented programming is a vast topic. 

Anyways my best interpretation of a class is: "Classes are constructs that have a set of defined methods, such that any objects instantiated from this class will have access to those methods." 

The methods that classes have are just functions that can be accessed with class_name.method_name(foo). In addition, there are special methods which Python can call for special operators. Special methods are denoted with "__" in front and in the back of the method name. Classes should at minimum have  __init__, __eq__, and __str__ special methods. 

Special methods:
__init__: Instructions on how to instantiate an object of the class
__eq__: Compares two objects and determines if they are the same
__str__: The str representation of an object of the class

There are also public and private methods. Private methods are denoted with "_" in front of the method name. There is actually no protection from someone calling them but serves to tell a reader that it should not be called.

Classes are useful since they provide a common API for various things that may need it. This saves a lot of rewriting code when they are inherited. This is called inheritance and will probably be talked about in a later SLOG.

Saturday, 13 February 2016

Week 5 SLOG - Linked Lists

We actually didn't learn much this week since Monday was spent to discuss Wednesday's test and assignment 1. As stated, Wednesday was the test. This week's lab and Friday's class were the only instructional  classes which we covered Linked Lists. The type of Linked List I'll talk about in this SLOG are singly Linked Lists.

The Linked List is set of nodes. Each node contains its value and the reference to the next node in the Linked List. This allows the nodes to be linked as if it was a list.

The benefit of a Linked List is that it avoids Python's directive to allocate large blocks of adjacent memory for long lists. This means that a Linked List should be able to be extended indefinitely until the computer runs out of memory as its nodes can be widely dispersed throughout the system. It also is beneficial when the list is going to be modified as removing an object from a standard Python List will shift every object that appears after the removed object one index forward. This makes it better than a List in implementing a Queue.

The cons of a Linked List include using more memory for each object in the list as it must also have a reference to the next node in the Linked List.

The benefits were best illustrated in Friday's presentation of a Queue and Stack implemented with List and a Queue and Stack implemented with Linked List. The running time of the Linked List implemented ADTs grew linearly with size increase while the running time of a Queue implemented with List grew in a quadratic manner in relation to size. The List implemented Stack grew in a linear fashion.

The benefit is seen only with large lists so I wonder if we'll be learning about asymptotic running time after reading week. In other news: there wasn't enough time for the test!!!

Saturday, 6 February 2016

Week 4 SLOG - Abstract Data Types

This week we learned about Stacks and Queues and Abstract Data Types. At first, Stacks seemed rather useless as they operate in the Last-In-First-Out (LIFO) order. Then Danny Heap brought up in class that stacks are used for coding programs itself, as they are used to add and resolve function calls. This was probably quite obvious but I didn't realize this. In hindsight, the python visualizer shown in CSC108 was definitely showing Stacks. 

The Queue is similar but instead it operates in a First-In-First-Out (FIFO) order and is the one that is very prevalent in the real world. With Tim Hortens lines (or any retail line) being a prime example. Both of these are called Abstract Data Types (ADT) which are when we represent real world phenomena in the computer world.

Some other common ADTs include Lists, Sacks, and Dictionaries. Lists and Dictionaries are inbuilt classes. Sack is not inbuilt and represents a sack that you throw things into such that you take things out randomly.

The implementation of most of the new ADTs can be done with various inbuilt classes such as List, Tuple, and Dictionary. In class, Stack was implemented as a List for simplicity and for the already inbuilt tools. Sack was implemented as a dictionary to take advantage of the non-ordered nature. Personally, I prefer using List due to familiarity and the useful tools .append(), and .pop(). 

To sum up, the list of ADTs this week are:
- List: Represents a set of objects in which any object in the List can be accessed and new objects can be added anywhere. Real world example: A list!
- Dictionary: Represents a set of objects in which a key is needed to access an object. 
Real world example: A dictionary.
- Stack: Represents a set of objects in which only the most recent object can be accessed. 
Real world example: A stack of paperwork.
- Queue: Represents a set of objects in which only the least recent object can be accessed. 
Real world example: A checkout line.
- Sack: Represents a set of objects in which produces a random object in the set when removing. Real world example: A lottery draw.

I'll probably add LinkedList next week, once we're done exploring it.