Sunday, 1 March 2015

Week 7: Recursion OR Object-Oriented programming OR Abstract Data Structures

Week 7

Object-oriented programming is a very convenient way to design a program or writing a code. It takes less time and the functions looks more precise. Few important topics we encountered in the course for last few weeks such as 'class', 'subclass', recursions, and 'trees' certainly was being more than helpful once we get familiar. 

'Class' is the most important blueprint in object-oriented programming. Class logically groups related functions together to create an object. The __init__,  __str__, __repr__,..., etc. method which defines the class and initialize the class. The subclass which I found is very convenient. This is also called class inheritance as it inherit the characteristics from a preexisted class and add additional functions to make the 'object' behave in a certain way. The subclass shows the characteristics of 'lazy geeks' or smart geeks perfectly. 

Recursion also plays an important role in object-oriented programming  Recursion saves a lot of work for computer scientists, it's a function calls itself. We did some exercises on input and output numbers using recursion. The very basic example nested list illustrated how recursion works in a simple and elegant way. I feel that recursion is a very interesting and convenient tool compare with iteration. However, tracing recursion was a bit confusing. We must trace the function calls in order, just like trying to understand how a computer thinks when calling recursion, step by step. For example:

  1. Trace nd([5, [[1, [2, 3], [4]]]]) 
         nd([5, [[1, [2, 3], [4]]]])
         --> 1 + max([nd(x) for x in [5, [[1, [2, 3], [4]]], ’ox’]])
         --> 1 + max([0, 3, 0]) --> 1 + 3 --> 4
    
This was an example we did in lab. The funny thing is I always have the impulsion to just sum up all the integers in the bracket. It's kind of ridiculous, but I think it's because I'm not used to recursive functions and concatenate the sub calls of the function itself. Recursive thinking might help. 

Later in the course, we also studied 'Trees', which combines the knowledge we learned from 'class' and recursion. Trees are a set of nodes with edges between. It's special characteristics which only have one path from the node to the root and no cycles make trees to represent inheritance hierarchical relationships in programming. It might be difficult at first when we try to get a certain node in lab exercises, its easy to mess up and went to a wrong path. However, once we get familiar with how the relationships in trees works everything became smooth.  



No comments:

Post a Comment