Monday, 6 April 2015

Slog # 10: The Finale

Unfortunately, like all good things, this blog is coming to an end. The course has reached its finale.

I found that my knowledge of computing in python really grew since the beginning of this course. I have learned a number of new things such as recursion, binary search trees, etc. and I have also developed my knowledge of classes, which I was introduced to before this course. Overall, I am very pleased with the development of my skills and knowledge of computer science and Python.


Until some day, wishing you all the best!

Feel free to leave a comment. 

Sunday, 29 March 2015

Slog # 9: Revisiting...

Revisiting Slog # 5:

Slog # 5 discussed the basics of recursion. At the time, I found recursion simple to trace but sometimes difficult to use, especially when writing code for trees. Now, I find recursion a very important part of coding and a key skill. While recursion was a very important part of the course, I have learned and now understand more topics in computer science. Looking at previous recursion problems that I found difficult, I see that I understand more but there will always be some recursion problems that may be more difficult than usual thus requiring more thought.

Overall, revisiting slog # 5 shows me how much my knowledge about coding and other topics in computer science has grown.


Until next time, revisit the old slogs!

Feel free to leave a comment. 

Sunday, 22 March 2015

Slog # 8: Binary Search Tree Mutation

As discussed in the last slog, mutation may occur in linked lists but it can also occur in trees. Let us consider a class for a binary tree node and a class for a binary search tree, as shown below.

class BTNode: 
' ' 'Binary Tree node.
' ' '

 def __init__(self, data, left=None, right=None):
 ' ' ' (BTNode, object, BTNode, BTNode) -> NoneType 

Create BTNode (self) with data and children left and right. 
' ' '
self.data, self.left, self.right = data, left, right 



class BST: 
' ' ' Binary search tree. 
     
      root: BTNode -- root of binary search tree 

      Assumptions: -- all data in root.left is less than root.data 
                             -- all data in root.right is more than root.data 
                             -- None indicates an empty tree 
' ' '

def __init__(self, root=None): 
' ' ' (BST, BTNode or None) -> NoneType 

Create BST with BTNode root. 
' ' ' 
self.root = root


In order to mutate this binary search tree, certain methods must be used.

In class, we went over "insert" and "delete", as shown below.


def insert(self, data): 

' ' ' (BST, object) -> NoneType 

Insert data into this BST, if it is not already in it.
' ' ' 
return_node = node

if not node:
     return_node = BTNode(data)
elif data < node.data:
     node.left = insert(node.left, data)
elif data > node.data:
    node.right = insert(node.right, data)
else:
    pass
return return_node


I found this an important example to work through because most mutations follow this pattern with "if node None", other cases, and "else". Working through this example and then practicing another mutation code are very helpful to understand the pattern in code for mutation.

Overall, binary search tree mutation code does require a bit of practice, especially once the codes become more difficult.


Until next time, mutate trees!


Feel free to leave a comment. 

Slog # 7: Linked Lists

Linked lists are connected to the topic of trees in computer science.

A linked list is a LINEAR SEQUENCE of NODES! (Hence the connection to trees)
The front of the sequence may be referred to as the HEAD, while the back of the sequence is referred to as the TAIL.

It is important to be able to understand the meaning of a linked list, which for me was cleared up by the following:

"a linked list:
          - a value and a sub-list (the rest of the linked list)
          - a sequence of nodes, each with a value and a reference to the next node"

In the examples I examined, a linked list as whole may be called a "wrapper" class because it can bundle information about the whole linked list.

Just as trees were traversed, linked lists may also be traversed. The common code for traversing a linked list in python is:
     current = self.front
     while <some condition>:
               <do something to current node>
               current = current.nxt

It is important to note that mutating code for linked lists, if gone too far, one may make the necessary changes in the previous node.

I found mutating the code interesting but not a topic that is comfortable immediately. Once I revisted the topic, my understanding improved.

Overall, linked lists are an interesting topic in computer science.


Until next time, keep linking!


Feel free to leave a comment. 

Slog # 6: Trees

Trees are an interesting and useful concept in computer science.

While a general class Tree may be allowed to an unlimited number of children per node, a BINARY TREE is a tree with a branching factor 2 thus having a LEFT and RIGHT child.


Tree Traversal

Tree traversal is the act of visiting nodes so an operation may be completed at each node. There are three standard orders of visiting nodes. They are the following:


  • PRE-ORDER traversal
  • POST-ORDER traversal
  • IN-ORDER traversal
Pre-order traversal means visiting a node BEFORE its children, while post-order traversal means visiting a node AFTER. In-order traversal means visiting a node IN BETWEEN visiting it's children, which works for binary trees.


Binary Search Tree

A binary search tree is a tree with a branching factor 2 and has a special property:
       "for every node, it's value is: - greater than all the values in the subtree
                                                      - less than all the values in the subtree"
It is important to note that a subtree of a binary search tree must also be a a binary search tree.


I find binary search trees quite interesting because of their special property. Binary search trees are interesting to set up if one is given a set of numbers and explained this property. I also found my recursion skills to be useful for tree code thus proving that it is quite important to practice this concept.

Overall, trees are an interesting topic in computer science and I look forward to learning more.


Until next time, climb that tree code!



Feel free to leave a comment.


Monday, 2 March 2015

Slog # 5: Recursion

In computer programming, recursion is the act of calling upon a function itself. Recursion, in python, may be a difficult concept to grasp at first as it may be replaced by iteration, but it is quite useful as it provides a concise and organized solution to certain problems.


Recursion is simple to use in basic functions such as a function that returns the sum of values in lists but it is also very useful for binary trees. For example, let's examine a code from class;

def contains(t, v):
    ''' (Tree, v) -> bool

    Return whether Tree t contains v.

    >>> t = Tree(17)
    >>> contains(t, 17)
    True
    >>> t = descendents_from_list(Tree(19), [1, 2, 3, 4, 5, 6, 7], 3)
    >>> contains(t, 5)
    True
    >>> contains(t, 18)
    False
    '''
    if t.value == v:
        return True
    else:
        return True in [contains(c, v) for c in t.children]


The solution to this code for "if", takes the "base case", which is an expression that does not require solution thus making it very simple. The "else" part of the code is the difficult aspect as it requires recursion. In this example, since the function takes two arguments, the recursive function must also take two arguments in order to work properly. While, this seems like a simple code to implement, it is much easier to understand the code when it is given than when one must write the recursive code. Practice is key!

From personal experience, I found recursion a reasonable concept to understand when tracing code and completing basic programming examples. I found recursion difficult once I had to use it in code, specifically when I had to implement a function called parenthesize, which parenthesizes the expression rooted at "b", where "b" is a binary tree. The parenthesize function is shown below.

def parenthesize(b):
    ''' (BTNode) -> str

    Parenthesize the expression rooted at b, so that float data is not parenthesized,
    but each pair of expressions joined by an operator are parenthesized.

    Assume:  -- b is a binary tree
             -- interior nodes contain data in {'+', '-', '*', '/'}
             -- interior nodes always have two children
             -- leaves contain float data

    >>> b = BTNode(3.0)
    >>> print(parenthesize(b))
    3.0
    >>> b = BTNode('+', BTNode('*', BTNode(3.0), BTNode(4.0)), BTNode(7.0))
    >>> print(parenthesize(b))
    ((3.0*4.0)+7.0)
    '''


While I found parenthesize difficult at first, I found that using the following steps helped:

1. First, find a simple example representing an expression that does not require recursion (This will probably be the "if" statement).

2. Next, find an example (not as simple as in the first step) that may be solved by using recursion (This will probably the "else" statement).

Combining these statements into proper python coding will, most likely, give the desired result.
By using these steps, and thinking through the recursion in the second step, I was able to solve the parenthesize problem.

Overall, recursion is a necessary skill for program, that may be difficult at times, but useful.


Please feel free to leave a comment!


Until next time, practice recursively!

Sunday, 22 February 2015

Slog # 4: Object-Oriented Programming Concepts


Object-oriented programming in python involves the use of "classes" and "objects. This is a very important topic in computer science.


A Few Concepts: 

It is very important to understand the difference between a method and a function. A function is code that is called by it's name while a method is called by the name associated with object (usually self). "Self" is the basis of the object-oriented programming in python covered thus far.

Other important concepts include understanding the _init_ method. This is a special method which creates a new instance of the class. Another special method is _str_ which creates a printable string representation, while _repr_ creates a string representation that can be evaluated.


Overall, these are a few important concepts to understand in object-oriented programming in python.


Feel free to leave a comment.


Until next time, learn about yourSELF!

Monday, 9 February 2015

Slog # 3: Recursion

Recursion is an important topic in computer science. A function is RECURSIVE if it calls itself. A substitute for recursion when solving problems is using "looping" (iteration).

Why use RECURSION?

While recursion may appear mysterious at first, it is quite useful in computer science as it is concise and elegant whereas a solution involving iteration is complex and inelegant.


First steps with RECURSION:

To understand recursion, it is important to first learn to trace a recursive call. Trace a few examples with the smallest possible input. Once confident that recursion works for all problems of this size, it is best to check the next largest input. Confidence is key!
It is also important to consider "size". When tracing recursive calls, smaller code creates problems and it is important to study/examine this behavior.


Overall, recursion may take time to understand but examples will help with understanding greatly!


Feel free to leave a comment.

Until next time, will you believe? 

Monday, 2 February 2015

Slog # 2: Impressions

Time has flown by quickly and many topics have been covered. There has been a development in understanding classes - parent and child classes, self, the _init_ method, _str_ and _repr_ method. Parent and child classes led to discussion of inheritance. While understanding this material is essential, it makes more sense when used thus emphasizing the importance of PRACTICE! 

Practicing a number of times with various examples is when one can truly understand the meaning of these topics. Practice means, at least, close to perfect (understanding)!




Please feel free to leave a comment. 


Until next time, keep PRACTICING!

Sunday, 25 January 2015

Slog # 1: Why geeks should write

Writing = Success Today

          Basic communication is an essential part of life. Today, as humans develop the world, there is an increasing need to be able to communicate in writing. What does this mean exactly? Writing in python? Writing in English? Actually, both. 

Python? English?
While programming, the way we choose variable names, class names, method names, etc. depend on English writing. We must be able to choose an appropriate name to convey a certain message thus "geeks" (or at least, computer "geeks") are familiar with the importance of writing. Since writing is already important for "geeks", surely any type writing can be learned and valued. Being able to communicate in writing is a useful skill, not just for a resume but also to develop yourself as an individual, specifically your thinking process.

Writing is a necessary skill (for everyone) to be successful today.

Please feel free to leave a comment.


Until next time, keep writing!