Monday, 30 March 2015

Week11

After completing assignment 2 and 3, I believe that I got a better understanding of recursion.
Looking back at slog of week 5,  I fount I kinda messed up recursion at that time. Back to week 5, I tried to understand recursion in a complex case as a whole, such as [1,2,[3],[45],[[4],[5]]]. Instead, considering a base case would be much simpler.

So a base case is when the code is as simple as we can answer it easily. If the base case is not fulfilled, then the recursion function would just trace the function again to its next level. Thinking in this way, recursion makes much more sense to me.



Sunday, 22 March 2015

Week 10


We didn't learn any new terms last week since we had our second midterm. We did some exercise on binary search tree.

When we inserting or comparing a value, we have to consider the property of a binary search tree- the value of left child is smaller than the node value, which is smaller than the value of right child. For examples, functions such as find_max would only consider right child while an insert function would compare the value to node value, left child value, and right child value respectively and then insert. 

Functions of a binary search tree are also recursive functions, cases should be considered thoroughly.  Cases such as the node has no left child, the node has no right child, the node has both children, and the node is None are all possible. 

Sunday, 15 March 2015

Week 9 -Linked List

A linked list is a set of nodes in a linear sequence. To understand it more precisely, we can view it as a sequence of nodes which contain a value and a reference to next node.





We define two classes here, LLNode and LinkedList ; represents a single node and the sequence as a whole respectively. 

Comparing with binary trees, traversal in linked list is much simpler. A linked list has a head ( or front) and a tail (or end). We need to assign the current node to the next node when traversing a linked list. 

Different from traversal, mutating can be complicated and easy to messed up. Mostly we have to find the code first, and then revisit the list to mutate it. References such as front node, back node, previous node, and current node are used quite frequently. 

Sunday, 8 March 2015

Week 8 - Binary Tree

Tree is not a new term to we computer science student. Even though we have learned some basic knowledge in CSC 108, we still have to clarify some terminology here.  A tree is a collection of nodes with an edge connects a parent and a child node. An internal node is a node with one or more than one children. Except the roots, each node has only one parent.

Tree is somewhat confusing. We have to be extremely careful with the code typed in python. For example:

L = [5,1,8,2]
t = Tree (3,L)
t.children [3]= Tree (9, [6,7])  

In this case, we are making a new copy of the list instead of changing list L. So L is still [5,1,8,2]. 

Tree is also linked to recursion.For a code like t.children [3]= Tree (9, [6,7]) , we have to precisely know which node is being replaced. Moreover, when we are given a docstring to write an if expression, we need to start with the simplest first. Then we can deal with a docstring like Tree (9, [6,7])   and even the question. 

A binary tree inherits all the properties of a tree class. However, it limits the number of of children per node to 2, which are called the left child and the right child. At this stage, we introduce a new term - binary search tree. Generally, any value smaller than node data goes to left child and any value greater than node data goes to right children.

There are 3 standard tree traverse orders: pre-order traversal, in-order traversal, and post-order traversal.






Pre-order traversal visits a node before its children. The order for the example is [F, B, A, D, C, E, G, I, H]

In-order traversal only works under binary tree. It visits left child first, then node, and right child. The order for the example is [A, B, C, D, E, F, G, H, I]

Post-order traversal visits a node after its children. The order for the example is [A, C, E, D, B, H, I, G, F]



Sunday, 1 March 2015

Week 7 - Abstract Data Type (ADT)

In Python or any other object-oriented language, Abstract Data Type (ADT) can be simply defined as classes. More precisely, it is a certain class or certain data types of similiar semantics. It focuses on the meaning of and the operation provided on the data rather than how it is implemented.Why abstract? Since it is not a basic type, such as str or int.  It solves practical problems freely based on our needs in the corresponding data types. In order to define a class, we have to define who is our client, a certain data type that provides abstract services we require, and the interface.

An example of ADT is stack, in which all datas correspond with FIFO (first-in-first-out) sequence. It stores data (input) and provides an operation to add and remove data (output). Input and output lead to changes in the internal state.

When implementing an ADT, we need to specify an interface. This clarifies and tells the user what the operation of ADT does. One of the significant features of ADT is that it combines attributes and methods under classes. This literally prevents the user from keeping track of separate pieces information on their own. In order to specify an ADT, we need to identify the attribute and the behaviour of the class we want to start with. Methods are functions defined within classes. They compute data and may change properties related to classes instance to satisfy the requirments for various problems.



Sunday, 15 February 2015

Week 6 - Object-Oriented Programming Concepts

Object-oriented programming is a design idea in programming.  OOP , stands for object-oriented programming, under which programming is based on the concept of "object" and " method", representing date structures and codes respectively. Since everything in OOP is grouped as an object, we do have reusability of data, such as we can access and modify the object.

Precisely, an object is something that can perform some activities, and the activities performed defines object. In python and other programming languages, classes are used frequently. Class is one type of object, quite frequently. Class is kind of like an blueprint in which individual objects are created.

Since programming software requires hundreds, or even more classes, some OOP concepts are introduced to help managing classes. For example, inherence , abstraction, interface,  and encapsulation. Among all the OOP concepts, we spend the most time on studying and programming on inheritance and interface. Inheritance is a powerful way for organizing programming software. A subclass can inherit behaviour and state from main classes. An interface is like a medium, connect classes and the outside world.


Sunday, 8 February 2015

Week 5

During this week, we are learning the usage of stacks and queue.  Stacks and queue are kind of similar to each other. Both of them are like a container of data, which is inserted and removed based on certain order. The difference is that while stack is based on a last-in-first-out (LIFO) sequence, queue is actually based on a first-in-first-out (FIFO) sequence.

At the beginning, it is quite easy when we are dealing with simply list, such as [1,2,3,4] and [1, [1,2] , 3]. However, when the list is getting complicated, it is easy to get confused. For example,  a list such as [ 1, [2,3], [ 2, 3, [4, [5, [7,8],6],[4,5]]] would be hard to trace. Complicated lists in stacks and queue are based on the knowledge of recursion. If we don't have a solid knowledge of how recursion works and how to trace a recursion list properly. we will certainly screw up on stacks and queues.