Saturday, 23 January 2016


Preface


This blog is created because I am taking CSC148 at the University of Toronto in Winter 2016.  As a fundamental course, CSC148 lays a solid base for the computer science (CS) undergraduate students. The topics covered in CSC148 includes: Object Oriented Programming (OOP), Recursion, basic Data Structure (Stack, Queue, Linked List, Tree and Heap), and preliminary Algorithm (sorting). With this course, the students will be able to learn advanced CS courses that deepen and broaden their knowledge ans skills in the CS.  Unfortunately this course is also a high threshold for those students who want to major the CS in the University of Toronto. Thus I expect this course will help me to enter the CS specialist program.

Currently the blog is used to share my experience of learning CSC148. I hope, however,this blog will be beyond CSC148 and even my study in the University of Toronto to  keep tack of my footprints in the computer science. 


Data and Data Type

 

1.  Data Type
The data in the real world are countless. How to process the data becomes the core of the Computer Science. The programmers produce numerous applications to help process data. When a programmer writes the codes for an application with Java, Python or C++, he assumes this application to process a lot of data instead of one by one specific data. This is why the data type is introduced.

A data type is defined as a collection of data that shares same attribute(s). For example,  a data type "Student" can be created to represent the data like "John A" and "Tom B". The number of data types is infinite because a new data type can be created whenever  mandatory and/or necessary. For the previous example, 'John A' and 'Tom B' might belong to another data type "Senior Student".

2. Abstract Data Type (ADT)

Most of data are meaningful as they are used  to correspond to real things. For example, "John A" represents a student. Accordingly their data types are meaningful like "Student". But some data are meaningless or abstract because they represent nothing at all. For instance, a data is ["John A", "Tom B"]. Obviously the content within the [ ] are meaningful. But can we say the data type is "Student"? It seems that this data is different from 'John A' and 'Tom B'. So the answer relies on what is the [ ]. To my knowledge, nobody knows its real meaning because it's just an abstract symbol. Of course, a programmer knows it represents a list and use to store data. The list ["John A", "Tom B"] results from the operation (storage in this example ) on the meaningful data "John A" and "Tome B".  Hence, we define an ADT like List to represent a collection of abstract data like ["John A", "Tom B"] with the same data structure and operations on the data.

Considering the universal use of  ADTs, most programming languages usually provides built_in basic ADTs (like str, int, list, dictionary and set in Python) from which the programmers can create more complicated ADTs. An ADT does not only represent a collection of abstract data but more importantly indicates a data structure. This is why the ADTs are very important to the programmers.

In the Object_Oriented Programming (OOP), a data type is created through defining a Class. This provides the programmers strong flexibility to handle a diversity of data with different attributes.When creating a Class, we must define its attribute(s) (called "class implementation"). It might be easy to define the attribute(s) for a concrete Class like Student, Course, and Department. But how about an ADT Class?  For example, we want an ADT class to store a course and registered students together. Notice this ADT Class is different from the meaningful class Course and Student because it's nothing until it is used to store the data from both Course and Student. In the non-OOP, we can directly assign an empty dictionary (a built_in ADT in Python) to a variable and subsequently assign keys and/or values to the dictionary as needed. The code would be:

course_registry = {}

def add_course(course):
            course_registry[course] = []

def add_student(course,student):
            if course in course_registry.keys():
                   if student not in course_registry[course]:
                                course_registry[course].append(student)
 

In the OOP, all the same jobs are finished through defining an ADT class Course_Registry. The code would be:

class Course_Registry:
   
    def  __init__(self):
           self.course_registry = {}
       
    def  add_course(self,course):
           self.course_registry[course] = []
           
    def add_student(self,course,student):
           if course in self.course_registry.keys():
                  if student not in self.course_registry[course]:
                                self.course_registry[course].append(student)
              
We implement this ADT class with nothing but an empty Dictionary (i.e., self.course_registry = {}) whose data structure is best suited to this example. But the more important work is to define the methods (like add_course and add_student) to provide an interface of operating the ADT Class. So we can get that the attribute of an ADT Class is nothing but a data structure indicated by the ADT. Notice that, in this example,  why the Class is named after Course_Registry other than Dictionary is because its use is limited ( just serving the class Course and Student). But its data structure is the same as a Dictionary.

The interface of an ADT Class determines the operations on the objects (i.e., data) of the Class , which makes this Class different from another even they have the same data structure. For example, Stack and Queue may have the same data structure List but they have difference interface. Stack always removes the element that is most recently added to the list (called "Last In First Out " (LIFO)) but Queue removes the element that are most early added (called "First In First Out" (FIFO)).      




End of this post.