Friday, March 7, 2014

Week 7: Recursion

During the past few weeks, we have been focusing on the concept of recursion. Recursion is a powerful and useful tool as it helps reduce and simplify coding in Python or be used to model and simply real world problems. In terms of python programming, recursion is basically when a function or method calls upon itself in its body code. For myself, the concept of recursion was not too difficult to grasp as it is for writing recursion code itself. Generally, it is somewhat easy to distinguish when to use or when it is appropriate to use recursion. If the problem at hand has a general pattern (i.e. Having to do a process or action multiple times in the same manner) then that is a good indication that recursion would be useful.

In terms of writing actual recursive functions and methods, this part is a bit tougher. However, there are many ways to simply the process and make writing recursive structures easier. Emphasizing again recursion is where the function or method calls itself x number of timed depending on what is passed into the parameters. This means that there will be x number of "steps" or results. One might think, "how do you know if each step/call is producing the correct value/answer?or wouldn't it be hard to track all the values/variables that each step/call produces?". In order to wrote code to makes sure produced answers are correct is to start off with the simplest case. The simplest case is usually when the function or method should stop calling the recursive structure (i.e. The function has no need to call the recursive structure and just return a simple value/None, requiring no recursion. This simple can be referred to the base case and it required for a recursive function to work. If a base case is not defined then one problem would be that the function would endlessly continue to call itself, or as Python would like to put it, "maximum recursion depth exceeded!". The base case is also a good steeping stone to begin writing difficult recursive functions.

Since the base case is the simple case where if recursion is not needed ( the function doesn't need to call itself again)  then the base case is executed. When starting off with the simple and continue with recurs ion and calling the function x numbers of times, we automatically know intermediate answers/values (i.e. In a nested list, we are able to identify the answers of the nested items without having to trace through them). Therefore recursive enforces a "lazy" approach.

Thursday, January 23, 2014

ADT and Recursion

ADT

ADT or abstract data type do not spedicty the implementation of the operations, unlike concrete data types, but they do specify a set of operations or methods and what they do. A common ADT is the stack ADT as shown in class. The stack adt has push and pop methods, which means that it is a specialized list where we can only access the most recent item that has been added or been removed from the list.

Recursion


Wednesday, January 22, 2014

Object-oriented Programming

Being relatively new to programming, the concept of object-oriented programming does seem to be quite intimidating. In my previous CSC course, objects (instances of classes) and their methods was introduced to us but it was not covered or focused on in great detail and therefore my comfort level of writing classes is not where it should be at. In spite of that practicing by writing will only improve my efficiency of writing code but also get a better understanding of classes to method interactions. (i.e. not forgetting to put self as a parameter when defining a method of a class).

Object-oriented programming can be seen as focusing on the creation of objects which contain both data and functionalit. These object defintions can coresspond to real world objects or concepts and the functions that operate on these objects represent the way that real world object interact. 

By week 3, we have jumped into topics on inheritance. At first the concepts did seem quite confusing at first but after going over notes and readings, understanding why inheritance is important and why it is used was key to understanding the concept. Some of the things i learned are:
  •  if one wanted to modify an existing class to change or add functionality, one cannot just directly make changes as it would break the existing clients. 
  • one of the solution would be to create a subclass of the base class (i.e. class Parent(object): would be the base class while class Child(Parent): would be the subclass).
  • by default, the Child class will inherit all the methods from the Parent class and therefore the Child like is exactly the same as Parent class except with the desired changes applied such as adding or modifying behaviour.
  • adding or modifying behaviour in the Child class can result in overrides. This means that whatever desired changes to methods and behaviour in the Parent class can be applied to the Child class by naming the method with the same name in which the Child class will override the Parent class.
What comes after concepts? Coding of course.  Aside from solving the problem as hand, wiritng the bodies of the methods of classes, was much the same and understandable. However, with the problem of defining a new Stack class that handles only integers, errors are often going to arrive when the user inputs a non integer. However, this time i am the one designing the error that is being raised. This is of course something new to me and i quite enjoyed knowing that we were able to raise our own exceptions, even though the steps to creating our own exceptions was incrediable easy, as it only consisted of creating a new class named whatever exception name we wanted and putting in pass.