Monday, 16 March 2015

Week 9: Linked Nodes and Linked Lists

As I mentioned on the last post, we will discuss about Linked List Node and Linked List. 
Basically, Linked List is a list of multiple Nodes linked together.
If you’re about to picture what Nodes look like, it will be like simple diagram below.

17| à 5| à 28| à None

when single Node looks like,

17| à None

As some of you can already picture, shifting previous Node’s pointing to the next Node is how to link Nodes.  If you want to visualize,

17| à None   &    5| à None    get linked together, 17’s arrow will have to point 5
17| à 5| à None

In python, class Linked List Node initialize with two values, current and next.

class LLNode:
    '''Node to be used in linked list
    nxt: LLNode -- next node
    None iff at the end of list
    value: object --- data for current node
    '''

    def __init__(self, value, nxt=None):
    ''' (LLNode, object, LLNode) -> NoneType
    Create LLNode (self) with data value and successor nxt.
    '''
    self.value, self.nxt = value, nxt  

Now, the Linked List has two attributes: front, and back.
Front refers to the beginning value and Back refers to the end value of the list.
You can access middle values by doing front.nxt.nxt.nxt … and so on.

When you code for Linked List, you’ll do (I’ll use same example I used above)

LL.front = LLNode(17, LLNode(5, LLNode(28)))
LL.back = LLNode(28, None)

Say you want t access LLNode(5, LLNode(28)), all you have to do is put ‘.nxt’ after.

>>> LL.front.nxt == LLNode(5, LLNode(28))
True

But what would the nxt refer if it’s used after ‘.back’?
As you guessed, yes it is None.
Why?
Because back refers to the end value of Linked List and last Node points to None (it must!)

Now, if you want to get the value of 5 or any other value of LLNode, all you need to do is type ‘.value’ after.

>>> LL.front.value == 17
True
>>> LL.front.nxt.value == 5
True
>>> LL.back.value == 28
True
>>> print(LL.back.nxt.value)
None

If you can create a list but cannot modify it, list won’t come useful to you.

There are many ways to modify list or search items in list.  I will provide initialize method of Linked List and try to test yourself how much you understand the material.

Whoever reads my blog, comment below your own code that functions as follow:
 a)    contains : checks if the value you want to search is in Linked List.
 b)   delete_back: deletes the end value
 c)    append : add new Node at end
 d)   push: add new Node in front

If you want a CHALLENGE, try to write a code that
 e)    prints all the values in list backwards.

Hint)
B – make sure you modify size of list as well. (self.size doesn’t change automatically)
C – same thing as mentioned in B, and make sure the back end points to None
D – don’t forget to modify size, and try point Node you want to push to rest of values existed in list
E – Recursion.

Besides, drawing diagram REALLY helps a lot.

The beginning code for Linked List is here:

class LinkedList:
    '''Collection of LLNodes
    front: LLNode -- front of list
    back: LLNode -- back of list'''


    def __init__(self):
    ''' (LinkedList) -> NoneType
    Create an empty linked list.
    '''
    self.front, self.back = None, None

    self.size = 0

The answer for all will be posted next week. Also, if you face an issue while coding, search in YouTube or ask Google; they are very helpful!

Saturday, 7 March 2015

Week 8: Binary Tree

In this week's slog, I'm going to talk about what we learned last week, Binary Tree Node. 
Binary term will be very familiar to techy person and yes it has to do something with 0 and 1.
Binary Tree can contain two children per node: left and right. 
Which means, initializing method will look like this:
class BTNode:
    """
    Binary Tree
    """
   
    def __init__(self, data, left=None, right=None):
        """(BTNode, object, BTNode, BTNode) -> NoneType
        """
        self.data, self.left, self.right = data, left, right

If we’re about to picture what BTNode looks like figuratively, it will be similar to the diagram below.
            self.data
               /      \
   self.left        self.right
   /          \           /         \  
left       right  left        right
(… and so on)

So now you guys are familiar with Binary Tree Node, let’s say this binary tree contains its element(integers) under the rule “parent node of two children nodes must be in between of two children’s numbers”.

In other words, if two children nodes have value of 4 and 8 respectively, their parent node’s value must be greater than 4 and smaller than 8.

In code wise, example should look like this: BTNode(7, BTNode(5), BTNode(9))
Maybe if we want to go deeper, we can try with 
BTNode(7, BTNode(5, BTNode(2), BTNode(6)), BTNode(9, BTNode(8))).

While you go deeper in Binary Tree Node or any other tree lists, you need to keep in mind that the rule must apply to every trees(even children’s trees).
That is why under BTNode(5), left node is 2 (smaller than its parent node 5 and 5’s parent node 7) and right node is 6(bigger than its parent node 5 but smaller than 5’s parent node 7). 
Moreover, same deal in BTNode(9): its left child has value of 8, smaller than 9 and bigger than 9’s parent node 7 since 9 is right child of 7.

Now, tree can be used in so many different ways but our coding for creating tree lists will be useless if we don’t have a code to evaluate things.
So, we will come up with a function that figures out whether BTNode contains the value we want to search or not. 

Basically, we want to figure out body function of below.

def contain(node, value):
    """(BTNode, object) -> BOOL
   
    Return a Boolean value indicating whether tree rooted at node contains value or not.

    >>> contain(None, 5)
    False
    >>> contain(BTNode(7, BTNode(5, BTNode(2), BTNode(6)), BTNode(9, BTNode(8))), 2)
    True
    """

First of all, if there is none of node to search in the answer will be False so,

    if node is None:
        return False

Second, this is where we need to use recursion method since we need to compare every node’s (even children’s) children values to search.  Meaning,

    elif value < node.data: #want to search left node since left is smaller than parent node
        return contain(node.left, value) #search the value by using recursion function
    elif value > node.data: #want to search right node
        return contain(node.right, value)#Also recursion incase children has its own BT
    else: #value is equal to node.data
        return True

Therefore, complete function of contain will look like:

def contain(node, value):
    """(BTNode, object) -> BOOL

    Return a Boolean value indicating whether tree rooted at node contains value or not.

    >>> contain(None, 5)
    False
    >>> contain(BTNode(7, BTNode(5, BTNode(2), BTNode(6)), BTNode(9, BTNode(8))), 2)
    True
    """
    if node is None:
        return False
    elif value < node.data:
        return contain(node.left, value)
    elif value > node.data:
        return contain(node.right, value)
    else: 
        return True

There are so many ways to use the Binary Tree list as and it is all up to coders(you guys!) who will create them. 
Hopefully you guys have a better understanding of Binary Tree List and next week I'm probably going to talk about Linked List.

Keeping slogs is actually a great way to review class materials and I believe this will help me to get higher marks in the test.

Sunday, 1 March 2015

Week 7: Objective Oriented Programming (To be graded)

Object oriented programming is a must known programming style in order to be a better programmer.
Many people recognize this term by thinking of class and subclass, and that is correct.

Object oriented programming is a programming style where programmer initializes data structure, data type and functions that can be applied to data structure while coding.  (For detailed version of this go to the website:  http://www.webopedia.com/TERM/O/object_oriented_programming_OOP.html
OR the wikipedia site: http://en.wikipedia.org/wiki/Object-oriented_programming)

The benefit of object-oriented programming is that there is no need to create new modules every time when new types of objects are added.  If certain objects share same functions or methods, it can be stored in superclass and programmers can use subclass to specify certain functions or methods that make them to be used in their unique ways.  

Object-oriented programming is used big in languages Java, C++, Python, and more. 

In my opinion, object-oriented programming makes coding much convenient and more organized.  
There is no need to create classes every time I need new objects even though some aspects or functions of them can be shared.  

Relating to my course that I'm taking (CSC148), here is an example of convenience of using object-oriented programming:

class Student:
    def __init__(self, age, number):
        “””
        “””
        self.age = age
        self.number = number

    def get_age(self):
        “””
        “””
        return self.age

    def get_number(self):
        “””
        “””
        return self.number

class Jeff(Student):
    def __init__(self, age, number):
        “””
        “””
        Student.__init__(age, number)
       
class Jae(Student):
    def __init__(self, age, number):
        “””
        “””
        Student.__init__(age, number)

Not using OOP:

class Jeff:
    def __init__(self, age, number):
        “””
        “””
        self.age = age
        self.number = number

    def get_age(self):
        “””
        “””
        return self.age

    def get_number(self):
        “””
        “””
        return self.number

class Jae:
    def __init__(self, age, number):
        “””
        “””
        self.age = age
        self.number = number

    def get_age(self):
        “””
        “””
        return self.age

    def get_number(self):
        “””
        “””
        return self.number

As you can see, the more function subclasses share, the handier OOP comes in.  Example above only shares 2 functions so it is hard to notice but still you can tell non-OOP ones have longer code lines.  Moreover, non-OOP code lines increases gradually compare to OOP code lines (non-OOP needs to rewrite functions every time in need of new class).   


Therefore, a programmer who wishes to be a better coder and his/her program to run faster and better, he/she will have to be fluent in Object Oriented Programming.