Friday, 31 January 2014

Tower of Cheeses

Hanoi Solutions

Recursive solutions are easy to understand, but hard to implement. The reason is because it is easy to create an infinite recursion, or to get confused by nested conditional statements. The solution in Python is to use a list comprehension and perhaps a ternary else. This allows for compact and easily digestible code.

Tower of Hanoi

The solution to a 3-pegged tower of Hanoi is easy for the first n's, but as pegs increase, or as the amount of stackable objects increase, the complexity increases. A recursive solution is ideal for the Tower of Hanoi because the idea of moving a small amount of n objects, isn't much different from moving n+1,n+2,...,n+m objects, this is because the steps are the same for an increasing amount of objects.

Here is how to solve a 3-peg, 3 object Tower: A to C, A to B, C to B, A to C, B to A, B to C, A to B. It follow from an induction proof that n*n - 1 steps is the least possible for a 3-pegged Tower. We can use this idea to create a recursive solution and extend it to 4 pegs, or n pegs.

Thursday, 23 January 2014

OOP or ___ec_ o_i_nte_ _r_g___mi__

OOP

Object Oriented Programming refers to a programming paradigm that arose long before I was born, and will persist long after I am gone. The idea is to bundle functionality and data into Classes. Classes are the blueprints which you use to create a unique object that inherits all the functions and values of the class.

A good example is creating a Customer class, which can have attributes(values) such as: Balance, Credit, Orders, History. Also, functions in classes are called methods, which are specific to the class such as: add_balance, remove_credit, view_history. When you create a Customer object, it is said that you instantiate an object, which gives the object all the functionality of the Customer class(Methods, and Attributes). If we have customers Tom and Alison, then they will both have attributes: Balance, Orders, and the methods: add_balance, view_history. This allows Tom and Alison to be Customers, who share enough characteristics that an object oriented approach is justifiable.

Attributes & Methods

Attributes, as explained above, are simply values that are attached to a given object. We have a customer who has $50 balance, $0 credit, these are attributes of the object.

Methods are functions that are useable only by the class. We have a customer who wants to view their history to see their purchasing patterns. The customer simply uses the method view_history and can view the history associated with his object. In addition, the method for view_history would most likely have attributes that consist of a list of his recent transactions. Often methods and attributes are used together, although not always.

Inheritance

Inheritance is the idea of inheriting the methods and attributes of a given class. The idea here is that if you have two extremely similar customers, you wouldn't create another class again.

A good example is a Normal customer class(NCC) and a Business customer class(BCC). The BCC is a similar to NCC, but the BCC would need some unique features, perhaps: tax information, bulk-orders, contract information. In this case, it makes sense to define a new class, but since we want most of the features of the NCC, we inherit from the NCC. Now we create a BCC that has identical methods and attributes of NCC. Now we customize our BCC by creating attributes and methods that will supersede the NCC, or perhaps entirely new methods and attributes.

I like to think of inheritance as a "is-a,but" relationship, therefore a BCC is a NCC, but with extra features. Although, I am not sure what to do in the case where you want a class to inherit only specific features. In this case, perhaps we can define the methods and attributes and use the pass approach as placeholders.

Encapsulation

Encapsulation is the idea of only allowing methods to interact with a object. Certain languages restrict access and modification to an object, while others allow it, and hope the people using the program will understand. I agree with the former approach, it seems logical to disallow random functions to interact with objects, because it can lead to errors and frustration if someone isn't knowledgeable of how the code works. A neat idea is the public, private, and protected approach to objects that certain languages use. Although, I must confess I haven't thought much about encapsulation, which hopefully should change in the new few weeks.

Friday, 17 January 2014

Computer science, joy, and objects.

Computer Science

The first program I ever wrote was hardly anything at all, a function that displayed the sum of two numbers. This was remarkable to me; to realized that I had the ability to create things, instead of being just a bystander.

Joy

The wonderful thing about computer programming is that you can create things that people take for granted, such as: Interfaces, Cellphone apps, Social media websites, databases, and games. All of these tasks are unique and useful in their own way. The ordeal of programming involves failure, hope, frustration, pride, and ingenuity. Also, I believe programming enforces logical behavior, because it is necessary to think logically to make anything non-trivial.

Objects

Object is an ambiguous term, but it is usually implied in English that objects are tangible. This is in stark contrast to what objects represent in object-oriented programming, where an object is an abstraction or an abstract data type(ADT) that serves a purpose.