Sunday, 15 February 2015

SLOG4: Summary of Object Oriented Programming

Hi all, I am back with my 4th SLOG for this semester. I have to say things are going so fast but as I anticipated in my first slog, its going to be an uphill battle. It sure has been one. With test one, assignment one and a couple of quizes out of the way I feel much confident in my ability to program using python. Today I'll be writing more about what object oriented programming (OOP) actually is. In doing so, I hope that it will aid my understanding of OOP and will fill the gaps (if there are any) for your understanding. Please feel free to correct me and fill in where I am lacking by commenting at the bottom. One of my reasoning in my  "Why Geeks should write" slog was that writing helps the writer as much as it helps the reader (if not more) in understanding the topic at hand. With this reasoning in mind I will start by defining what object oriented programming is all about. 

Object oriented programming is a programming language model that focusses on 'objects' and data rather than action and logic. In OOP we care about objects to be manipulated rather than the logic behind manipulating them. Some of the basic topics of interest are:
1) Class: A data structure that contains the set of attributes that characterize any object of the class.
2) Subclass: child class. 
3) Class variable: A variable that is shared by all instances of a class. it is defined within a class but outside any of the class's methods. 
4) Instance variable: A variable defined inside a method that belongs only to the current instance of a class.
5) Inheritance: The transfer of the characteristics of a class to other classes that are derived from it. Subclasses are used for inheritance.
6) Instantiation: the creation of an instance of a class
and much more. While most of this was a review from CSC108, working on the assignment 1, creating classes and subclasses helped me be fluent in creating classes. Initially I was overwhelmed by the amount of things that creating classes require, specially the special methods __str__, __eq__, and __repr__. But now that the assignment is done and the test 1 is over with I am super comfortable with these. 
Looking back at the concept of object oriented programming, I can see how one can use this approach to keep track of data. This is extremely helpful to me for the statistics part of my studies. It helps me view material presented in other non-compsi classes as a class having subclasses. It helps me to use the idea of superclass and subclass and methods to summarize material in my other courses in a coherent manner and do a better job and understanding and remembering the material for later one, such as on a test. 

I hope this summary has helped you in understanding the object oriented programming concept more and I hope you are able to relate to the idea of how using OOP for summarizing other course materials can help narrow down to the main topics of interest rather than the 'junk' material. 





Thursday, 5 February 2015

SLOG3: Tracing Recursion

Week 4 SLOG 3 and still counting...
So turns out that I have successfully survived the first 4 week of classes. Did I just say 4 weeks?? wow that sure was fast.Anyhow, this fourth week was quite interesting I must say because it wasn't until the beginning of this week when I realized that the next week I was to be writing the midterm for CSC148. With the upcoming test preparation and the amount of stuff due in my other classes including Assignment 1 for this course, week 4 was a hell of a ride.

Now, lets talk about the interesting stuff. Or I should say, the new material we studied during week 4. We were introduced to Recursion. Recursion is a way of coding a problem in which a function calls itself multiple times in its body. For now we are asked not to worry about writing the codes for recursion and focus on tracing them. To be honest, this was the first time that after the lecture it felt like I actually knew what was going on during the lecture. I was actually able to follow along with the prof. I want to say its an easy concept but I dont also. The way Prof. Danny went over the material explain step by step how recursion works and how to trace it, made it easier for me to understand the material.

The code for Recursion is as followed:
              if isinstance(L, list):
                    return sum([sum_list(x) for x in L])
             else:
                  return L

Now lets look at how a simple list will be traced:
    sum_list([4, 1, 8]) --> sum( [ sum_list(4), sum_list(1), sum_list(8) ])
      # here we go over the code presented above to see whether sum_list(4) is a list or not if it is not we return the value of L and if it is then we sum the values of the list.
                                --> sum([4, 1, 8])--> 4 + 1 + 8
                                --> 13

A nested list would look difficult to trace, however it is quiet easy to trace:

        sum_list([[1, 2, 3], [4, 5], 8]) --> sum([sum_list([1, 2, 3]), sum_list([4, 5]), sum_list(8)])
         #here we add the integers of each list separately:
                                                    --> sum([6 (1+2+3), 9 (4+5), 8])
        #now we have a list of 3 integers that can be added by using the built-in function sum: 
                                                   ---> 23 (6+9+8)
I know it might be too soon to say this, but Recursion has to be the easiest material to grasp on for me. For me it was a very basic concept and something that I can actually use later on for coding. Also, since I actually understood the material, I actually enjoyed the lab of this week because I was able to finish the lab and do well on the quiz. Out of all the topics we learnt in class, my favourite topic would be tracing Recursion. Like I said in my previous post, "Practice makes a man perfect", this truly holds as the more I practice using the additional questions on the lab handout,  I better understood Recursion as well as other topics.

This is what I actually got out of Recursion, I would love to hear what you think and please feel free to add to my understanding of Recursion if there is anything major I missed on via commenting on this post. Thank You for reading my not-so-funny posts. I will leave you all with this pretty picture presented to us in class that used recursion: 'The Recursive turtle example":