Saturday, February 28, 2015

Summary On Abstract Data Types

One of the main objectives of a programmer is to create programs and code which is efficient, elegant and properly readable by the programmer him/herself, his/her colleagues and the clients. For this, one has to focus more on the semantics of the program than on its implementation, which he/she does by creating classes for a certain data type and by defining various methods that could be carried out on the data type. This is where abstract data type comes in really handy.

An abstract data type (ADT) is a data type where we specify the kind and meaning behind the data type we store, and various methods we can carry out on that data type. I can see three major advantages to using an ADT to solve problems in computing than the normal ways in which we solved things back in CSC108. They are:
i) Using the concept of ADT definitely makes the program look a lot more elegant and readable for    yourself, your peers and your clients.
ii) Since you don't have to focus on how the program is implemented, there is a lot more freedom in thinking about algorithms or new methods for the program.
iii) Lastly, through the use of ADT, one can easily carry out tasks like debugging, maintenance, updating the program, etc. in an efficient and organized manner, without disrupting the client code.

There are two main examples of ADTs that I would like to talk about. They are Stack and Queue. They are both similar in the sense that they are both implemented as a list in Python in order to pile up objects and remove them. However, the way they add and remove objects are different, and that's what I will be covering. 

Stack:
A Stack is a linear sequence of data types. In a Stack, new items are stored on the top (end of the list), and items at the top are the ones that are removed first. Think of it as a stack of new notebooks when you go to to the shop. Usually, shopkeepers add new notebooks to the top of the stack, and consumers take notebooks that are on the top of the stack. 
Some operations that can be carried out on the Stack ADT are:
i) Reverse the sequence of the Stack.
ii) Pop: Remove and return top item, provided the stack is non-empty.
iii) Push(item): Store item on top of stack.
iv) is_empty(): Return whether the stack is empty or not.

Queue:
Just like Stack, A Queue is also a linear sequence of data types. However, new items are stored at the back of the queue (end of the list) and items at the front (beginning of the list) are removed first. This means that in order to remove an item you just added in a queue, you need to remove everything you added before it. 
Some operations that can be carried out on the Queue ADT are:
i) enqueue(A): add object A at the back of the queue (end of the list).
ii) dequeue(A): remove and return object A from the front of the queue(beginning of the list).
iii) Reverse the sequence of the Queue.
iv) is_empty(): Return whether the Queue is empty or not.

I'd recommend checking out the week 5 blog in the link http://nigerianmahogany.blogspot.ca/. Here, the person has a visual representation of what happens in a stack and in a queue.

And that's about all I have regarding ADTs. I'm really enthusiastic towards learning about more ADTs, and I'll update you all if I learn more. Any questions are welcome and you can leave them in the comments below. Till then, keep on coding and stay awesome. Bye.

Saturday, February 14, 2015

My Summary Of Object Oriented Programming Concepts

So far in CSC148, we've been practicing object oriented programming (OOP) in Python, and although there's much more for us to learn regarding object oriented programming, I wanted to briefly summarize and explain some of the concepts we've learned so far in this field, and that's what today's blog is going to be all about. First of all, I'm going to give a brief introduction to object oriented programming and explain what exactly it means. Then I'll start explaining the concepts in OOP I've learned so far.

Ptting it in simple words, OOP (Object Oriented Programming) is a way of programming in which the user not only defines the data and its type, but also functions and methods that can be applied to it. This way of programming comes very handy especially when it  comes to abstract programming. For example, in our first week in CSC148, we made a class called Point where we create various methods and carry out various operations, based on an abstract set of co-ordinates given by the user.

Now, I will start explaining some concepts that I learned in object oriented programming.

1) Objects:

In OOP, we bundle up the data type and the methods applied to a data structure and call them objects, which are stored at a location in memory. In Python, every single thing from integers, strings to even classes, are objects. One of the advantages to this type of programming is the ability to import modules like math, str, unittest, etc., which make programming a lot more easier since instead of carrying out various operations on your own, you could already use pre-defined functions in a module.

2) Classes:


In simple terms, one can think of classes as a template or a backbone for creating objects, initializing variables and creating as well as implementing various methods. In out first week, in order to solve the abstract problem which I talked about in my explanation of object oriented programming, we created a class called Point where we initialized a variable name for the co-ordinates and started creating and carrying out functions like finding the co-ordinate's distance from the origin.

3) Inheritance:

I guess one of the cool things I've liked about Python so far is how you can create a new class that inherits objects and methods from the old class, thus making programming a lot more easier and elegant. This is what we call inheritance. In CSC148, we practiced inheritance by creating a class called employee which takes in details of an employee and returns their amount of pay and total pay. After that, we created subclasses called HourlyEmployee and SalariedEmployee which inherits methods from the employee class. This made the programming process, especially the process of initializing variables a  lot more easier since by inheriting the init function, one can avoid writing the same code over and over again which takes in a lot more time. This feature is especially important in cases where one has to create many different subclasses but carry out similar methods as in the parent class.

For further information regarding OOP, you can also refer to the link of one of my peers, which is https://humaircsc148slog.wordpress.com/2015/02/15/object-oriented-summary/.

And that's about what I've learned regarding object oriented programming that I can write off the top of my head. I know for sure that there's a lot more to learn regarding this way of programming and I look forward to it. Until then, keep coding and stay awesome.

Friday, February 6, 2015

My impressions on tracing recursion

On the 4th week of CSC148, many of us getting accustomed to programming were introduced to a technique, called recursion. In simple words, recursion is a programming technique in which a function or method calls upon itself in order to solve a problem. The purpose of this week wasn't necessarily teaching us how to code recursive codes, but more regarding how to actually trace recursive codes and predict the output to a recursive function or method,

On the first lesson, we were introduced to a programming style where you can carry out simple functions with items in a list or dictionary with just one line instead of creating loops and calling out each element in the list or dictionary. For example, let there be a list of integers named I. In order to square every element in the list, instead of calling out a loop, we can use the command [item ** 2 for item in L]. I really liked this method since it looks a lot more shorter and elegant rather than calling out a loop.

On the same day, we started learning how to recursive code. We traced a recursive function called sum_list, which takes in an integer or an arbitrarily nested list of integers, and returns the sum of all integers in it. While doing this exercise, there were two thoughts that came to my mind. The first one was that the same problem could have been solved by using more iterations or loops, but then I realized that it would be a very complicated process and would be very messed up to read when it comes to debugging the program. In that sense, I felt like recursion is a very powerful programming tool since it makes the problem solving process very short, elegant and readable. The second thought that came to my mind was the fact that just for this problem, one didn't have to undergo the rigorous process of recursion since all they had to do was get the sum of all the integers in the list, no matter how they are listed. However, once we started going in detail regarding how to solve it, I realized that I shouldn't rely on this intuition since it might cause errors in other recursive problems. Therefore, I told myself that I would properly follow the proper steps for tracing recursive functions. I also did a bunch of recursive exercises which helped me strengthen my recursion tracing skills.

On the next day, we applied the concept of recursion in order to carry out a sorting process called mergesort sorting. Just like selection or insertion sort, it is a way to sort numbers. The way it works is that it takes in a list of numbers, divides it into two parts. Then it applies the same process to those two parts and continues on until there are several lists which only contain at most 1 integer. Then, it sorts the elements in ascending (or descending, depending on user) order. Since you need to call the same steps over and over again, we decided to go about this using recursion, and it made the process a lot more shorter. It was a very interesting exercise. We also did labs where we further polished our recursion tracing skills.

In order to understand more about how recursion actually works, you can refer to week 5 blog of the link http://csc148slogz-z.tumblr.com/. The person here breaks recursion down and explains how recursion works in a program where we find the depth of a nested list. Furthermore, you can refer to the February 7 post of the link http://christianlessard.blogspot.ca/ where the person talks about why learning to trace code is important. For those of us who feel like giving up at the moment, this could be helpful to you.

Overall, this has been a very interesting week in CSC148 since I got to learn a brand new concept I had never even heard of before. It felt very thrilling to trace recursive codes. I look forward to learn how to actually code them efficiently. And just like recursion, I look forward to learning more programming techniques in this course and in the years to come. Till then my friends, keep coding and stay awesome. Take care.