Thursday, 24 October 2013

CSC148 First Test

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.


No comments:

Post a Comment