Tuesday, February 24, 2015

Week 1 to Week 6

Although I'm supposed to write one post per week, I prefer waiting for content to accumulate to be able to have quite the pack of content to explain and talk about. I just enjoy it that way.


Week 1
Quite the soft week as we were introduced to Computer Science in general and how different it was from just computer programming (CSC108) and the whole concept of solving problems using our acquired programming knowledge.


A few things that were discovered or clarified over the course of this week:
------------------------------
The work of objects and memory addresses. Objects being equivalent does not mean they are the same object. For instance. Assume two lists for the following scenario:
>>> L1 = [1, 2, 3]
>>> L2 = [1, 2, 3]
We declared two identical lists, however, if we were to use some Boolean operators, we'll find out how these two lists are different.
>>> L1 == L2
True
What we did in there was to check whether both lists are equal or rather "equivalent" as in, they have the same content.
>>> L1 is L2
False
The two objects may be equivalent and have the same content, but they are not the same object! Their memory address is not the same, thus False being returned.
Applying this in a context, we can compare the lists to boxes have objects in. The boxes are of same size and have the same content, they are equivalent, but the boxes are not the same one box. They are two different boxes having the same content in them.


Week 2
This week was a deeper introduction to ADTs, abstract data types are objects we create and modify and mold to our needs to solve certain problems.


One ADT we've learned about this week is the Stack. A stack is basically a list of objects where the only directly accessible item is the item on the top, thus the notion of stack. We add an element to the stack (on top of it) or remove the top element in question. The stack itself is created through the use of class implementation and the adding and popping the/to the stack is made through method within the function.


It is also good to implement other method for classes or ADTs that are the following:
__eq__: Method that allows the use of the == Boolean operator.
__str__: Method that allows a string representation of the object.
__repr__: Method that allows a representation of the object when called.


This was pretty simple, although one tends to set aside crucial notions such as these, which may be fatal later on.


Week 3
This week introduced us how classes inherit from each other. We will be talking about parent classes, child classes or superclasses, subclasses.
When we create a certain ADT, we can create other ADTs that inherit attributes, methods and others!


For instance, suppose we create a class:


class SomeClass():


   def __init__(self):
      attribute1 == []
      attribute2 == {}
  
  def some_method():


   ...


Suppose we want to implement a subclass, using the first one as a template.


class SomeOtherClass(SomeClass):


   def __init__(self):
       SomeClass.__init__(self)


...


The subclass SomeOtherClass inherits of all the superclass' attributes as well as methods in that case and can have additional methods to make it more unique in functionality or fit a certain case of a problem.


Here comes two issue at this point, although using classes and subclasses can be useful, there are cases where you want to make sure certain class attributes or methods cannot be used or modified by the local interface or in some other classes, cannot be used with the superclass and requiring a subclass to function.
------
In order to prevent a certain method to be used with the superclass, we raise an exception error at the method in question and design the method as it should work in the subclasses.


def some_method():


   raise ExceptionError("Needs a subclass.")
-----
When wanting to prevent methods from being modified, we had the __ before their name in the definition.


def __some_method():


    ...


Many of these have to be taken into consideration as we do not want the user to temper with the functionality of the ADTs.


Week 4
Recursion was introduced to us this week. To explain what is recursion, it is the act of recalling a certain function or method within itself for a specific looping condition. We mostly learned how to trace the call at each iteration to obtain the wanted result or obtain a result. The tracing part is extremely important and required quite the practice as failure on a single iteration might generate a false result.


Week 5
Test week! No real content taught in here, though the test was quite manageable, I could have done better if only I knew how to recall the inherited __init__ method, added doctstring, traced well one recursion and placed an attribute in the parent class instead of the child class.


Week 6
Although a very difficult week for me, we were introducted to trees. An abstract data type that takes an object followed by another tree or nothing on top of using them for recursion problems.



No comments:

Post a Comment