Sunday, 15 February 2015

Object-Oriented Programming

Object-Oriented Programming is simply the manipulation and interaction of different objects. It has many benefits, including eliminating the need to write duplicate code. A good example of this is the creation and interaction of classes, the blueprints for objects.

If I wanted to emulate the workings of a school, I can create a few classes to represent this. For example I can create separate student, and teacher classes.

class Teacher(object):
 
    def __init__(self, name, subject):
        self.name = name
        self.subject = subject

class Student(object)

    def __init__(self, name):
        self.name = name

Bob = Teacher(Bob, English)
Joey = Student(Joey)

Now I have created (a representation of) a teacher named Bob who teaches English and a student Joey.
But hold on, since both a student and teacher are people, I can create a class named Person and have the Student and Teacher class inherit from that Person class. This way, if I ever wanted new Student and Teacher attributes, like something universal like age, sex or height, I would only need to edit the parent class, Person, instead of editing several different classes.

class Person(object):

    def __init__(self, name, age):
        self.name = name
        self.age = age 
    
class Teacher(Person):
   
    def __init__(self, name, age, subject):
        Person.__init__(self, name, age)
        self.subject = subject
       
class Student(Person):

    def __init__(self, name, age, grade):
        Person.__init__(self, name, age)
        self.grade = grade


Since a teacher and a student are both people, I can simply construct a parent class called Person with attributes that are common among all people, like age and name. Then, in the Teacher class, we need to know what subject that teacher teaches, but also by inheriting from the Person superclass, we can ensure that the teacher has a name and an age. And for students, a unique attribute may be what grade that student is in. So we first simply inherit from the superclass to ensure the student has a name and age, and then create a new attribute called grade in the constructor of the Student class.

Object-Oriented Programming is very intuitive and saves a programmer a lot of work in terms of writing efficient code.


Sunday, 8 February 2015

Week 5 SLOG: Recursion

I find recursion to be really fascinating. There are three principles for a recursive function:

1. There must be a base case.
2. The function algorithm must tend towards that base case.
3. The function must call itself.

An example of a recursive function is a function that calculates factorials.

def factorial(n):

    if n == 1:
        return n
    else:
        return n * factorial(n-1)

>>> print(factorial(5))
120

As one can observe in this function, the base case is when n is equal to 1, in which case it will return n, or else it will move towards that base case by multiplying n when it's not 1 with the factorial of n-1.

Recursion looks like it is a staple of computer science and problem solving in general.



Sunday, 1 February 2015

My Impression of the First Weeks of CSC148

In these first few weeks of class, we reviewed basic concepts from CSC108 and covered new subjects; the major ones that I believe are: list comprehension and stacks

List Comprehension
List comprehension is a fantastically efficient way to create a list in Python. It encompasses the simplicity, efficiency, and readability one would expect in Python.

For example, if we needed a list of the powers of 2^n, from n = 0 to n = 9, we can construct the list with list comprehension, instead of writing a function for it.

List Comprehension
>>> [2 ** n for n in range(10)]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]

Function (Doing the same thing)
def powers_of_two(n):
    lst = []
    for x in range(n):
        lst.append(2**x)
    return lst

>>> powers_of_two(10)
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]

Stacks
Stacks are a type of abstract data type in which the last item in is the first item out. An example of this is a Python list, in which when you call append on it, you add the item you want to append to the end of the list. And then when you call pop() on the list, the last item of that list will be removed.

>>> stack = [1, 2, 3]
>>> stack.append(4)
>>> stack
[1, 2, 3, 4]
>>> stack.pop()
4
>>> stack
[1, 2, 3]

Of course, this is only one example of stack. One can even construct a stack as a Python class and then manually implement the methods of pop and push.