So the first test was last week, and after completing it I thought I had done fairly well. The first two questions were straight forward. The third, however, was slightly confusing. The problem was to create a "FunctionalList" class, which returns a copy of a FunctionalList using slightly altered append and sort methods. Constructing the actual class did not seem to be a difficult task. But as I started to write my constructor, I realized the implementation would be a little bit tricky (at least for me). Two possibilities came to my mind, I could either implement the class by creating an instance variable which stores all the data (i.e. a list). The second possibility was to extend the list class and make a super call in the constructor instead of having an instance of a list. I decided to go with the latter. After all a "FunctionalList" is itself a list, so inheritance seems to be the way to go here (in fact the question made this clear). But this raises another question. How would the super call in the constructor be made? Is it actually possible to create a list like so:
a = list([1, 2, 3])
Well apparently this is possible, except I have never seen it before because almost nobody ever initializes a list like this. This is one of the reasons I found this question interesting. I've never actually seen a list initialized in any other way than the quick, double square bracket notation. Anyway, if this is the case then the super call in the constructor should look like:
super().__init__(data), where 'data' is a list parameter taken by the constructor.
This looked plain weird to me, but I reluctantly wrote it on my test anyway. I wonder how other students reacted to this question. Did anybody else find themselves having the same problem, or was it just me? Perhaps there is an alternative way to solve this problem which I have not considered, let me know in the comments.
Thursday, 24 October 2013
Saturday, 12 October 2013
Recursion & Object-Oriented Programming
Recursion is a method of solving a problem by firstly solving smaller instances of the same problem. In computer science, a recursive algorithm is one which calls on itself until a base case is reached. Recursion is very important for computer scientists, as it usually provides an elegant solution to many problems which could otherwise be very complicated to solve. For example, I often play chess and I recently found a problem which can be solved using a recursive algorithm. The problem is known as the "Eight Queens Puzzle". The problem is to place eight queens on a chessboard such that no queen can attack another queen. The recursive algorithm uses backtracking (once a conflict is found, the computer will go back to the last proper state and try to find a different combination). There are many other applications of recursion. This includes binary search, recursive data types (trees), fractals and much more!
Object-Oriented Programming (OOP) is also very important for computer scientists. An OOP language allows programmers to write classes which serve as blueprints, or templates for creating objects. This is very similar to real life. To build a radio, for example, a manufacturer would firstly create a template for the radio and then give this same set of instructions to a factory to mass produce. If the company wanted to make a different radio, then they would likely use a slightly altered version of the original template rather than constructing an entirely new template. This same idea is used in programming. Building a radio object would firstly require writing a class. Building a different radio object would require reusing the same class, but altering some of it (this is where inheritance comes in). This is highly advantageous for computer scientists as it does not require a lot of code to be repeated (much cleaner). Another advantage of OOP is information hiding (encapsulation). Going back to the radio example, an average consumer who buys a radio can use it and not worry about how the radio actually works as it comes with an interface (power button, volume control, etc.). This same advantage is present in computing. Programmers need not know how an object works, they just need to know that it does work (before OOP, coders needed to know how a piece of code works before using it). Clearly, OOP makes our lives easier when we are coding. It makes our code cleaner, reusable, readable and generally easier to work with.
Object-Oriented Programming (OOP) is also very important for computer scientists. An OOP language allows programmers to write classes which serve as blueprints, or templates for creating objects. This is very similar to real life. To build a radio, for example, a manufacturer would firstly create a template for the radio and then give this same set of instructions to a factory to mass produce. If the company wanted to make a different radio, then they would likely use a slightly altered version of the original template rather than constructing an entirely new template. This same idea is used in programming. Building a radio object would firstly require writing a class. Building a different radio object would require reusing the same class, but altering some of it (this is where inheritance comes in). This is highly advantageous for computer scientists as it does not require a lot of code to be repeated (much cleaner). Another advantage of OOP is information hiding (encapsulation). Going back to the radio example, an average consumer who buys a radio can use it and not worry about how the radio actually works as it comes with an interface (power button, volume control, etc.). This same advantage is present in computing. Programmers need not know how an object works, they just need to know that it does work (before OOP, coders needed to know how a piece of code works before using it). Clearly, OOP makes our lives easier when we are coding. It makes our code cleaner, reusable, readable and generally easier to work with.
Subscribe to:
Posts (Atom)