Thursday, January 30, 2014

Week 4: Recursion and nested list

This week we learnt how to find the depth of nested list in a recursive way. It is the first time for me to learn recursion, it seems much more simple than for loop or while loop, because its results are always very short, maybe in one or two lines. But it is hard to figure out how it runs for me.

Let's look at an example first(which we learnt in class):














All recursion need a base case and a recursion step.

What I think difficult to do is to build the recursion step, it is really hard for me to find the relation ship between the base case and the current case. I was struggled for a long time on e2.

But fortunately, one of my friend taught me that I shouldn't think too much on the whole procedure, just figure out the relationship by simplify the current cases.

I did so, and was surprised to find I am done, I made it. It is no doubt that recursion is not that difficult, just analyse in a right way, every one can grasp it.

Thursday, January 23, 2014

Week 3 : About Object-oriented programming

In Wikipedia, the definition of Object-oriented programming is showed below.

"Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs.[1][2] C++,Objective-C, Smalltalk, Java, C#, Perl, Python, Ruby and PHP are examples of object-oriented programming languages."

It has a series of basic theories, like class, subclass, method and so on.

Just as we learnt in Python, a class is defined as "an extensible template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions, methods)".

For example, we defined two classes as Toy and Dog in the exercise we did before. We made 'Bob = Toy()', then Bob belongs to the class 'Toy', it has the implementation of behaviour 'play',which means it can be 'play'ed.In the example above, we can also define other two things as object and method. 'Bob' is one of the Toy, it has its own properties, by which I mean, it is unique over the world, and we call it 'Object'. Maybe 'Bob' have many common properties with other 'Toys', but it is itself. Different from the abstract 'class', it is a specific concept. Just like 'class' is a set of all things around the world containing some things which have similar properties, but 'object' is the particular element in the class. As for the 'method', it defines many things that 'objects' in 'class' can do(but not have to). Let's back to the example above, all 'toys' have the 'method' play, if we make 'Bob.play()', it will return 'Squeak!\n'.

These are the basic definitions in the OOP.

I think it is a very convenient way to create a new class, for dealing with problems more specifically and easily.