Friday, 27 February 2015

Week 7: My summary of Abstract Data Types

           Stack, an abstract data type(ADT) was formally introduced to me during week 2 lectures by Professor Danny Heap.  Later, during tutorials and lectures, I also became familiarized with two other ADTs called queue and tree. Although I understood the application of stacks and queue, I still could not understand why they are called abstract data types. Moreover, I was unable to discern the difference of ADTs from other built-in data types such as list, dict, str and tuple, etc,. Hence, in today’s slog, I will be giving a detailed summary on ADT as well as introducing the three examples of ADTs I encountered in class.

          First of all, an abstract data type is a model for a particular data structure defined by the methods it can perform. One thing to keep in mind is that not only can ADT be implemented in python, it can also be implemented in other programming languages like C, java, etc,. This is because ADTs do not contain detailed codes on their implementation. In other words, ADT is concerned only with what the data is representing in general but not the the specifics for its construction.

          Now, I will discuss the definition and purpose of stack. A stack is used to store countless data in an order in which new items are added to the top of the stack, while the top item always gets removed. Hence the acronym LIFO, which stands for “last-in-first-out” is used to describe stack.  Furthermore, stack includes three basic operations. The first method is push(item), where the new data is added to the top of the stack. The second operation is pop() which functions to remove the top (also last) item from stack. And the last method that I was introduced to in class was isEmpty(), which returns True if the stack is empty, else False if the stack contains at least one item. Although Danny did not teach the class all the methods related to stack. I was able to learn them on my own due to my incessant curiosity, After researching online, I discovered that stack also included other operations such as peek(), which returns the topmost item from stack without making any modifications to the stack, and size(), which returns the number of items in the stack. Lastly, real life applications of stack are stacking of plates or books. To see the example calls on these methods, check the link:

          The second ADT that I learned is queue. And I learned it in a tutorial as I was told to write my own code on implementation of queue. Similar to stack, a queue also stores data. However, it operates in a FIFO fashion, which stands for “first-in-first-out”. It includes three basic methods (these are the methods I was asked to write during my CSC148 lab). The first operation is enqueue(item) that adds a new item to the the rear of queue. In other words, as items are being added to a queue, the first item is being pushed to the top. Thus, the last added item is at the bottom of queue, which is different from that of stack (as aforementioned, the last item to be added locates at the top of the stack). The second function is dequeue() that serves to remove the first and topmost item from the queue. Lastly, similar to stack, queue also has a method called isEmpty() that returns True if queue contains no data, and false otherwise. Moreover, queue also has the operation size() that returns the number of items stored in it. The real life applications of queue are people lining up to board an airplane or buy tickets from a ticket booth. Lastly, its example calls can be viewed by clicking the link: http://interactivepython.org/runestone/static/pythonds/BasicDS/queues.html


          The third and last ADT I learned is tree. To begin, similar to stack and queue, tree also stores data. However, it stores data in a hierarchical order. Each item/object is called a node, and we refer to the bottom nodes that happen to branch out the top node as the children of the top node. Furthermore, nodes without any children is referred simply as leaves, whereas, the top most node is called the root. Functions for trees that I was taught in class and tutorials included determining the height of the tree, the number of children, etc,. So far, the class only looked at binary trees (trees where a node only has a maximum of 2 children, called the left and right subtree) in great detail. To sum up, the ADT tree is a real world tree flipped upside down. In my CSC148’s assignment two, I will be combining recursion with tree to assess every possible outcomes of the Tippy Game, and then return the best strategy for the player to win the game. (This idea is required in the class called StrategyMinimax).

          In conclusion, throughout these 7 weeks of class, I learned about abstract data types and its three specific examples — stack, queue and tree. Up until now, the most difficult ADT I learned is tree. However, I have no problem understanding its structure and implementation, since my quizzes about count_leaves is perfect, and I was able to complete the labs regarding trees in a efficient and speedy manner. One thing about tree that I enjoy is its frequent application of recursion. This is because I find writing recursions challenging yet satisfying at the same time after reaching a solution. Hence I was elated to find out that I will be combining both tree and recursion in Assignment 2’s StrategyMinimax. So far, my group has completed the codes for TippyGameState and TippyMove, and we will be writing StrategyMinimax tomorrow. Hopefully, my group can finish the GameState by Monday. Lastly, the advantages of using abstract data types is that ADTs can be reused in future programs. Further,  their implementations can be easily modified without having to make troublesome changes to the program that uses the ADTs. 

Thursday, 19 February 2015

Reading Week: Other CSC148 students' slogs

          During reading week, I had a lot of spare time, so I decided to read other students’ slogs. After going through around 20 other slogs, I have picked my favourite top three bloggers from the 20 blogs I have reviewed so far:


          The authour of the above mentioned blog is my favourite blogger so far. My reasons for liking her (the blogger could be a boy, but I just assumed she is a girl) is that she excel in consistency, writing, and knowledge. Firstly, she updates her slogs weekly with consistency. Not only did she wrote a slog during reading week, but she also wrote two different slogs for week 5! (I think she actually enjoyed writing slogs.) Secondly, her writings are impeccable. I enjoyed reading her slogs because they rarely contain any grammatical error. Furthermore, she always put in a lot of effort in making the slogs seem less boring and more interesting to read for the readers. I liked how she creatively posts interesting yet relevant pictures to complement her slogs. (I think I will adopt her idea.) Lastly, she can clearly define and explain programming concepts. Her explanations on recursion was brilliant and it demonstrated that she actually learned and mastered the material well.

2. http://egq148.blogspot.ca/
          My second favourite blogger is Haley (I think this is her name). Similar to the first blogger, Haley’s writings are excellent (although a grammatical error does appear occasionally) and her slogs are filled with useful information. Although like the first blogger, Hayley is also able to keep the readers entertained, her method of entertainment deviates from blogger number 1. Instead of using pictures to keep things interesting, Hayley writes in an engaging way—its as if she is having a conversation with the reader about her opinions and thoughts on CSC148.

          My third favourite blogger has to be the person with the student number 1001429162 (Since no name is mentioned in this blogger’s blog, I will simple refer to him/her as 162). What I liked about 162’s slog is that he/she has no grammar mistakes (I really enjoy reading writings that contain no grammar mistakes, since I can focus on the content instead of constantly trying to figure out what the writer is trying to say). Also 162’s slogs are really informative in that they contain clear and detailed explanations. In conclusion, I would suggest 162 to upload some pictures along with the writings like the first blogger.


          In conclusion, from reading these three slogs, I now know how to improve my future slogs. Firstly, I would engage the readers by posting interesting pictures and occasionally write in a conversational style. Next, I would present more useful and academic information in my slog, while explaining the difficult concepts in a manner that is easy for readers to understand. Lastly, I will check my grammars constantly in order for readers to smoothly and speedily read through my slogs with clarity.

Friday, 13 February 2015

Week 6: My summary of Object-Oriented Programming concepts

          When I first started programming in high school, I was introduced to the concept of object-oriented programming (a.k.a. OOP). Initially, I had to memorize the definition of OOP in order to pass my computer science exams. However, I had never really understood what OOP means and what it does. Now that I entered university studying in the computer science major, I felt obligated to master all programming-related concepts. So, after doing some research on the internet, I have summarized three important Object-Oriented Programming concepts that programming beginners should become familiarized with.

          First of all, one cannot leave out the definition of object. Now, object means different things under different contexts, however in computer science, it has only one definition. A software object is a model of real world objects, and similarly, a software object also shares two important characteristics, which are: state(or properties) and behaviour. In OOP, objects are created for programming purposes, and their behaviour consists of a set of methods/functions that the certain object performs, whereas the state encompasses all the objects’ properties and their associated values. Interestingly, an object is referred to as the instance of a class. And this leads us to the second OOP concept.

          A class is a representation of a type of object. In other words, it is similar to a blueprint that records all the instructions required to create a specific type of object. It consists of three characteristics:  name, attributes and methods. Since each class is designed for different purposes and contains different attributes, a program that is built to run certain application involves the implementation of countless different objects. Here is an example of a simple class
called Tiger:

Name Of Class: Tiger
Attributes: claws, fangs, golden fur
Methods: eat , roar, run

In this example, the attributes or properties of Tiger are its claws, fangs and fur, while the methods that the class encompasses include eat, roar and run. Since an object is the instance of a class (which is a specific object built from a certain class),  a tiger named Tina is an object created under the class Tiger. And Tina inherits all the necessary attributes and methods from its parent Tiger. Again, this brings me to my next concept.

          The last concept involved around the idea of inheritance of a class, which is the ability of a new class, when created, to inherit all the attributes and methods from an existing class. The new class is called the subclass, whereas, the original class is referred to as the parent class (or super-class). Using my previous example, I can create a new object called FemaleTiger, which is a subclass of the parent class Tiger. Like Tiger, class FemaleTiger also has attributes: claws, fangs, golden fur. Similarly, FemaleTiger also extended all the methods from Tiger: eat, roar and run. Furthermore, an advantage of using OOP is that not only can the subclass inherit functions from its parent class, the subclass also has the ability to add new attributes and methods to itself by overriding existing ones. For instance, subclass FemaleTiger can add another attribute, female, to itself. Furthermore, FemaleTiger can also override the methods it extended from Tiger, and replace them with: drink, give birth, breastfeed, and sit. Hence, my new subclass FemaleTiger will end up looking like this:

Name Of Class: FemaleTiger
Attributes: claws, fangs, golden fur, female
Methods: drink, give birth, sit, breastfeed

Likewise, I can create other subclasses like MaleTiger and BabyTiger:

Name Of Class: MaleTiger
Attributes: claws, fangs, golden fur, Male
Methods: drink, hunt, mate

Name Of Class: BabyTiger
Attributes: claws, fangs, golden fur, tiny stature
Methods: drink, sleep, play


          Although there are more things to say about OOP, such as interface, package,  encapsulation, and polymorphism, the rest is not currently related to my study and their information goes beyond my present knowledge. Thus I will conclude my summary of the OOP and discuss its relation with my Assignment 1. In A1, I was asked to create a generic game state class and later write out a specific game state subclass designated to the game Subtract Square. Despite having understood the general concept of OOP, I failed to properly divide the attributes between the parent and its child class. In other words, I did not properly include certain attributes that are common to all the specific games into the general game state superclass. I also realized that my general game state class mixed some specific attributes from the game subtract square. Hence, I want to remind all the programmers that despite how elementary the concept of inheritance sounds, applying it to programming is no simple tasks. I would recommend everyone to start creating classes with a piece of scrap paper, where they can jot down some similarities between the subclasses, and then include them in the general class. 
   
          In conclusion, OOP has many advantages, such as the ability to provide a clear modular structure for programs. Also, OOP is good for defining abstract data types (such as stack) and it allows easy modification and maintain of existing code as new objects can be created in a efficient manner using inheritance. However, despite all the pros, there are some cons associated with the application of OOP, and they are the fact that the size of object-oriented program tend to be larger, hence the large size slows down the execution speed of programs. Fortunately, recent improvement in compilers mitigated the problem.




Friday, 6 February 2015

Week 5: My impressions of tracing recursion from Week 4

          When I was first introduced to recursion, I could not comprehend the code. The reason was that I had never seen a function that implements itself within its own code before. And when I was asked in class to trace a recursive function, I failed to do so step by step. Fortunately, my confusion was cleared when professor Danny taught the class step by step regarding tracing recursions.



          In order to show my gradual process of ultimately understanding the concept of recursion, I will begin with a recursive function as an example: 


















(Note: this function is taken from additional exercises from Lab #3)
Now, I will trace all four example calls and explain my thinking as I go. 

          To begin, I will trace count_lists([2, 1, 3]). When the list [2, 1, 3] is being inputed into the function “count_lists”, the first “if” is evaluated. Since [2, 1, 3] is indeed type list, the “isinstance()” method is evaluated to True, and the statement “return 1 + sum([count_lists(x) for x in L])” is next to be implemented. Now the trace looks like this:

count_lists([2, 1, 3]) —> 1 + sum([count_lists(2), count_lists(1), count_lists(3)])

This is because the list comprehension applies the function “count_lists()” to every element in list [2, 1, 3]. Since 2, 1 and 3 are non-lists, the “isinstance()” evaluates to false and the code under “else” gets executed instead. As a result, sum([count_lists(2), count_lists(1), count_lists(3)]) becomes sum([0, 0, 0]). Hence, after simplification, the entire trace process will end up looking like this:


          Next,  I will trace count_lists([2, 1, 3, ‘[4]’]). Now, the tricky part of this trace is that ‘[4]’ is type str not type list, hence count_lists(‘[4]’) will return 0 just like the other three ints.


          The previous two examples were really simple and I had no problem understanding them. However, the next two example calls are a bit more complicated to trace since it involves inner-lists. 

The hardest part to understand would be the second line. Hence I will rewrite the first line into this:

1 + sum([count_lists(7), count_lists([2, 1, 3]), count_lists(9), count_lists([3, 2, 4])])

Since 7 and 9 are non-lists, count_lists evaluate to 0. However, because the second and fourth elements are both type lists, count_lists will evaluate to 1. One thing to notice is that count_lists([2, 1, 3]) and count_lists([3, 2, 4]) are simple recursions like example call 1. Since we have traced example 1, there is no need to rewrite it for this example call, and we can simply write 1.

          The last example call is the most complicated of the four. Fortunately, we can skip some intermediate steps when tracing since we did similar ones in the previous three examples. Looking at the example call, I can quickly spot that evaluating count_lists on element 7 and 9 is easy since they simply return 0. Furthermore, because we traced example call 1, we know that count_lists([3, 2, 4]) returns 1 + sum([0, 0, 0]) and hence 1. The most difficult part is evaluating the second element in the example call, since it contains an inner-list within an inner-list: [2, 1, [3, 4], 5].  This is the part where I had trouble the most. In order to fully understand this example call, I decided to break it down into simpler parts. I have already evaluated the first, third and fourth element, thus, I will simply trace count_lists([2, 1, [3, 4], 5]):







Now that I have evaluated the example call by breaking it into simpler parts that I have traced before, I can combine my solution:







          In conclusion, my initial reaction to tracing recursion is confusion. However, after patiently tracing all the practice exercises, I can confidently say that I have understood the concept behind recursion and if I am given a recursive function, I can easily understand its purpose by tracing several example calls. Although recursion requires more memory than iterations and it is slower to run in general,  it is nevertheless useful. The reasons are that:

1. Recursion presents a more elegant and succinct code for implementing a variable number of    nested loops, since the code usually only take up one line 
2. Recursion reduces time complexity 
3. Recursion makes tree-related coding simpler 

Therefore, I find recursion to be a very interesting topic to learn, and I hope that I will get some practice on writing recursive functions. I have a hunch that recursion is going to play a very important role in Assignment 2.