Sunday 13 October 2013

Recursion

Recursion is where the solution for a problem involves calling on the solution many times to solve the problem. A recursive solution uses a base case, where if the solution satisfies the base case, the recursive aspect of the solution will not be called into play, and the program will exit. However, if the base case is not satisfied , the recursive loop will be called and the process will repeat again, where the base case is checked.

Recursion can help when you have to navigate and search through lists, lists of lists , trees and those types of data types. With large recursive processes, where the solution has to be called many times, recursion can be less efficient however.

Object Oriented Programming

Object oriented programming is a useful way of organizing code and representing data. The basics of object oriented programming (OOP) are to create a class, which is an instance of an object. A programmed class has it's own variables and methods associated with it.
For example, a car is a class, made up of various parts and attributes such as the number and type of tires, horsepower, torque, gas mileage, etc. An example in code would look like this:

class Car(object):
def __int__(self, gascapacity):
self.numwheels = 4
self.wheeltype = "offroad"
self.gascapacity = gascapacity

def gasmileage(Car.gascapacity):
#calculations here

As you can see from the above, very basic example, classes and object oriented programming allow for instance variables as well as methods to be defined and contained within an organized class, thereby containing various calculations and functions within a single "object".  The main benefit to programmers is that objects can be programmed and then used wherever needed. In my example, a Car object that defines a car and calculates gas mileage could be imported in a variety of projects, and isn't restricted to just one person.

Another benefit to OOP is the ease of expansion. It would be very easy to modify the above Car class to include other information and methods, such as a method for calculating when wheels need to be changed or to tell when oil levels are low.

All in all, object oriented programming allows for ease of organization, better and cleaner sharing of premade code for use in other projects, and allows for more functionality to be added as needed.