Saturday, 14 February 2015

Summary of OOP Concepts

Well, well, well...
How coincidental is that I am writing on the first day of reading week and the evening of Valentine's Day? 

#Sad life of a (U of T)student...

Since it's such a special day, let me have some fun before I start rambling about objected oriented programming!  

After surviving the past few weeks, I just feel so obliged to share this with you:


Okay, back on topic...

Summary of OOP Concepts

A world of classes, objects(data types), interface, and abstract data types. 

To be honest, I am not the best at summarizing things...
I prefer to write essays instead.
 :) But I will try my best here. 

OOP, also known as objected oriented programming, is a programming paradigm based on objects(bundles of bits) and data rather than logic. 

Back in 108, we mainly exercised in procedural programming, where we focus on writing functions(procedures on data). 

OOP opened the gates to a vast new world, one that is full of complexity and data. The essence of OOP lies in higher level thinking about the organization and structure of data. The creation of classes allow the creation of many objects and a useful interface; we can thus create distinct entities that have their own functionality and data. 

In class, we have mostly emphasized on class design and inheritance. 

Class design can be a nebulous concept because there are so many ways to design a class! You can choose to only have parameter self in the constructor and have data storage internalized, and have methods for manipulation; on the other hand, you can choose to have different parameters for user input. 

But I believe it is always useful to ask yourself a couple of questions before you write a class:

1. What kind of data are you processing? 
2. What kind of objects would you want? Look for key words!
3. What attributes do you want for your objects? For example, a turtle class would want to have a tail as its attribute. 

Inheritance is what it suggest; it is the idea of having a child class "inheriting" attributes from the parent class. 
To illustrate its connotation in the English language, check out the classes I have here:

class Coco:
''' Coco is the parent of Joan'''
    def __init__(self):
        self.hair_colour = 'blue'
        self.eye = 'black'
        self.wings = 'green'

class Joan(Coco):
''' Joan is the child of Coco'''
    def __init__(self):
        Coco.__init__(self)
        self.wings = 'orange'   # I am a defiant DNA. 

Basically, Joan inherits all attributes of Coco(blue hair, black eyes) but may not be identical (have orange wings) as Coco. 

To further illustrate the utility of OOP in general and summarize it, let us take a look at another real world example. 

Mr.Potter recently opened a restaurant on Bay Street; he then wonders how he should process orders of his numerous customers without abusing his employees' memory capacity. 

So Mr.Potter can construct a class named OrderSystem and a class Order. OrderSystem can have attributes such as active_orders, implemented_orders, total_customers, and various methods. Class Order can have more specific attributes such as customer_name, table_number, time_of_order, and total_price. With each new order, a new object Order is created and stores the information of that specific table. 

i.e.
class OrderSystem:
    ''' A data system that stores information of orders.'''
    
    def __init__(self):
        
        self.active_orders = []
        self.implemented_orders = []
        total_customers = 0 
        
    def add_customer(self, name):
    
        pass
        
class Order(OrderSystem):
    ''' Class for specific order, inherits from OrderSystem.'''
    def __init__(self, customer_name, table_number, time_of_order):
        
        OrderSystem.__init__(self)
        self.customer_name = customer_name
        self.table = table_number
        self.time_of_order = [time_of_order]
        self.total_price = 0
    
    def add_dish(self, dish, order_time):
    
        pass

Personally, I think OOP is a very valuable, intriguing concept that is very practical. When solving problems in the real world, we often face a large pool of data(whether it be a list of patients, data tables of weather patterns or bank account data). OOP in turn allow us to store and process this data in a fairly organized manner. 

Also, as I was walking around my classmates' blogs, I found this classmate of mine, (http://whydopigsquack.blogspot.ca/) wrote a very nice summary of OOP concepts. Hence do keep in mind that if you do not find mine most helpful, you can always take a look there.  :)
You can also consult http://openbookproject.net/thinkcs/python/english3e/index.html , which I draw some of my information from. 

---------------------------------------FLYINGFISH's FINISH LINE--------------------------------------------
Thank you for reading! If you are nice, please comment below so I know you are there! :)


Saturday, 7 February 2015

['R',['E'],['CU'],'R',['SION']]


RECURSION
  RECURSION
       RECURSION
           RECURSION
              Aren't I bored? 


How we met recursion:

















"The magic of recursion lies in its ability to perform countless computations with just a few statements."
                                                              - Flying Fish (CSC148)

When we were first introduced to the concept of recursion through readings(the one about fractals), I, along with some classmates, found it fairly confusing. 

However, I managed to grasp the idea with the help of the fourth lab and more recently, the fifth. 

Instructors have defined a recursive function as one that is able to call itself over and over again. 

Though if I am to summarize to my best ability in form of analogy, recursion is similar to opening one of those layered gift boxes, which I used in middle school for 'dramatic effects' at my friends' Birthday parties. 

Here's the closest picture I can find:

# Mine weren't so fancy; and, I usually prefer to have multiple boxes(or small bags) at one level to make sure my friend will not get to the gift too easily.

Until the gift is found, the recursion can keep calling itself(that is, to continue opening boxes).

To put it in code:

def find_gift(L):

    if isinstance(L, gift):   # pretend gift is a type in python, just like list and str.
        return L
    else:
        find_gift(x) for x in L


At first, tracing some functions in lab 4 for nested lists was very confusing. However, I later realized what I was doing wrong after consulting my professor during the office hours. Frankly, I usually over traced and spent lots of time because I did not use the traced results from previous steps.
After learning from my mistakes, I had fun tracing more recursive functions!

In lab 5, I wrote list_queue and did tracing on paper.

def list_queue(lst, queue):
     

    for element in lst:
        queue.enqueue(element)
         
    while not queue.is_empty():
        d = queue.dequeue()
        if isinstance(d, list):
            list_queue(d, queue)
        else:        
            print(d)

As I drew things in piles on paper, everything came to sense! 
For instance, a list such as [1,[3,5],[4,[5,6],8],9]


1  ------> print(1)
[3,5]
[4,[5,6],8]
9 --------> print(9)
3---------> print(3)
5 ----------> print(5)
4-----------> print(4)
[5,6]
8-------------->print(8)
5--------------->print(5)
6----------------->print(6)



---------------------------------------FLYINGFISH's FINISH LINE--------------------------------------------
See you next time! (zzzzzZZZzzzzzz)