Saturday, 28 February 2015

Mark Tracing Recursion

For this week of slogs I have chosen to have my previous post on tracing recursion to be marked. 
Therefore, please refer to my post on Tracing Recursion, which was posted in the earlier weeks.
Thank You

Sunday, 15 February 2015

Summary of Object-Oriented Programming


Hello to all my readers,

First of all I just want to say that I survived my hectic week, all the tests, assignments, quizzes and labs. Phew. It feels so much better to have those out of the way only to have more things coming up though. *sarcasm*. 

Anyways, let’s gets back to CSC148 and this week I would like to discuss the topic of Object Orientated Programming (OOP). OOP is based on the concepts of objects, which provides an efficient model for programming. Classes of great interest in OOP, which allow the programmer to design various types of things from cash registry as shown as an example in class to creating game similar to what we did in assignment 1. Classes can create instances of itself using the constructor __init__ which can be created by the programmer; however, python also has a built-in __init__ method. Other built-in functions include __str__ , __repr__ and __eq__. The methods written in the specified class belong to that class, which allow the user to do various sorts of cool things including changing the instances of that class which had been created. 

Furthermore, a subclass also known as child class inherits all the methods of parent class. The concept of inheritance is critical to OOP and it is of the key reasons as to why OOP is very helpful. Without this concept, programmers would have to waste their time writing the same methods again in another class whereas inheritance as the name suggest allows the programmer to use the methods defined in the parent class in that of a child class. No need to the same work twice. New objects therefore can inherit the attributes of the existing or parent object, a major plus point.

That was all for this week. I shall come with another cool post about CSC148. Till then to my readers. Adios Amigos!!!

Thursday, 5 February 2015

Tracing Recursion


Recursion, recursion, recursion.


This picture nicely sums up recursion. We were introduced to the topic of recursion in the fourth week, which in simple words means a function calling itself. Fortunately, I did not have much difficulty tracing the recursive code in class and therefore I believed that I had grasped this concept. I was so wrong. One example is not sufficient to grasp any concept then how did I foolishly believe that it was enough for recursion. This concept is fairly easy however requires a lot of practise in order for one to be fully confident about it. Recursion is like puzzle that challenges you and being the competitive person that you are, you will most likely say, “You are on.” I can't believe I just wrote that but you will understand that this is definitely as the case as I tell you more about this beautiful world of recursion.


We traced the following example in our class.
def sum_list(L):
'''
(arbitrarily nested list of int or int) -> int
Return the sum of all ints in L.
>>> sum_list([1, [2, 3], [4, 5, [6, 7], 8]])
36
'''
if isinstance(L, list):
return sum([sum_list(x) for x in L])
else:
return L

 
The function sum_list(L) returns L if it is not a list or it returns the sum of all integers in L.
Therefore, tracing sum_list(27) will execute to 27 because 27 is an integer and NOT a list.
Moving on, lets trace the following sum_list([4, 1, 8]).  Since L is a list in this case, we will perform the function sum_list on each of the item in L. Thus, sum_list on 4, 1 and 8. Then since these items are integers the function sum_list will return their value evaluating to sum([4,1,8]), which eventually evaluates to 13.  Below is a better representation of what I just explained.


sum_list([4, 1, 8])       -->sum( [ sum_list(4), sum_list(1), sum_list(8) ] )
-->sum([4 ,1,8])
-->13


Similarly, lets trace sum_list([1, [2, 2], [2, [3, 3, 3], 2]]).
sum_list([1, [2, 2], [2, [3, 3, 3], 2]])    à sum([sum_list(1), sum_list([2,2]), sum_list([2, [3, 3, 3], 2])])
-->sum([1,sum([sum_list(2), sum_list(2)]), sum([sum_list(2), sum_list([3,3,3]), sum_list(2)])])
-->sum([1,sum(2,2),sum([2, sum([sum_list(3), sum_list(3), sum_list(3)]), 2])])
-->sum([1,4,sum([2,sum(3,3,3),2])])
-->sum([1,4,sum([2,9,2])])
-->sum([1,4,13])
-->18

Once you complete other such examples, some steps that I did in this example can be omitted because you would know that you have already performed in the steps thus no need to write it again. Who likes to do the same thing over and over again? Where is the fun in that right? If I by any chance lost you in the example, do not panic and do what I did. PRACTICE, PRACTICE AND PRACTICE.



For me I find that it becomes a bit easier to trace a recursive code, once you understand the function of the base case because other cases generally repeat the action of the base case. In the above example the base case is the instance where you begin with an L that is a non-list, what do you do then? You simply return the value of L. Then you would trace another simple case where you begin with L that is a list but only contains 1 item. Then the function simply perform sum_list on that 1 item and returns it. Then you would move on to something that we traced earlier sum_list([4, 1, 8]) where you simply perform the function sum_list on each of the item but if the statement requires you to further complete an additional step, which is to return the sum of list.  My classmate Andrew also finds it helpful to understand the base case to be able to trace recursive code efficiently.  In the lab this week, we traced more recursive codes, which was extremely beneficial in sharpening my tracing skills. I can now confidently say that I would be able to tackle a recursive code based on my practice. My classmates impressions on recursion are somewhat divided.  Nicole in her blog shares similar experience as mine because she also feels tracing recursion to be simple if one were to understand the general case then slowly move on. 

I shall take your leave now and write to you soon hopefully regarding more interesting concepts to come in CSC148. Till then everyone. Be sure to share your experience with recursion and how you overcame your difficulties if you had any.

Sunday, 1 February 2015

Few studying tips

To all the readers sorry for writing so late. I have gotten way too busy this week due to all the coursework, upcoming midterms and test. This coming week I have three midterms, a lab report, 2 quizzes and an assignment. With this amount of work, I cannot think on what to write other than share a few tips to students in similar situation as me. I would say that the best thing to do is get started early and not procrastinate, I know that things are easier said than done. Staying on top of materials most definitely helps because when you go over the concepts again you feel much at ease and when you might have many tests lined up up you don't feel as stressed. The class discussion forums such as piazza are furthermore extremely helpful if you need help with certain topics or have any course related questions. Many students may also have the same questions as you, so be sure to check those out when you are having difficulties with some concepts and such. Finally, be sure to also take breaks because our brain does not stay focused for a long time therefore breaks are really essential for grasping the material. These were just a few of my tips on effective studying, be sure to share yours in the comment section below. :)

Impression of first few weeks of slog


               
Hello readers,

It's seems to be a pretty good semester so far however this semester most of my classes have many assignments, quizzes and labs compared to the usual past semesters where I had 1 or 2 term tests then only a final exam. The term test is just around the corner and I am in an extremely tough position because along with CSC148 I have two more midterms scheduled this week so 3 back to back midterms plus 2 more next week. WOW. These 2 weeks will definitely be extremely hectic but the thought of reading week is somewhat comforting. Emphasis on somewhat. I say that because similar schedule is going to repeat after the reading week.

Anyways, let's get back to my journey in CSC148.  Initially, I was really worried about this course because of the rumours floating around that CSC148 is far more difficult than CSC108. These few weeks are not enough to grasp the difficulty of the course, but I continuously tell myself that if I were to put my hard work I should be able to do fairly well in class.


Of course the picture is a bit of exaggeration, but I would definitely need to put more effort compared to my peers who have a strong background in programming. My journey so far in the course has been full of ups and downs. For my first two weeks I was enrolled in Diane’s section however, due to my course schedule changes I later enrolled in Danny’s evening section. Turtle is what completely went on top of my head when I first sat in Danny’s class only because Diane’s section did not use turtle. So it was like a whole new world. 


However, I decided that I needed to go back to Danny’s previous two weeks lecture to comprehend the turtle examples. Furthermore, I approached my lab partner who was also in Danny’s section and with her help and by visiting the previous weeks lecture notes and example I was finally on the same page as everyone else. In these weeks we also learned various new concepts including inheritance, and list comprehensions to name a few. List comprehensions is especially interesting which is not exactly a novel concept but it a concept of doing things the efficient way in coding.  A four line code that we would have wrote in CSC108 is simplified into just one line. Just one line.

For example,
L = [2,5,8,10]
Returning_ans = []
For item in L:
            Returning_ans.append(item**2)
Return Returning_ans

Can you believe that this above code can be written in just one simple line. Yeah that’s right. Just one line. Voila !!!!!
[item ** 2 for item in L]

List comprehensions as you can see follow this form:  [<expression>  for <variable> in <iterable>].
I am highly impressed by how the labs correspond to the lecture material in this courses. The quizzes in labs were okay in general such that doing the lab material would be sufficient to get you a decent mark. The second quiz however was worded very awkwardly and therefore it was very unclear as to what was asked of the students. I have read in Varun’s blog and when speaking with other students that they also found the second quiz very confusing.  In general course notes and the lectures are very helpful in learning the concepts. Furthermore, it is more essential to revisit the notes, lecture examples and even do extra practice problems to actually grasp the concept. You know what they say, “Practice makes perfect.” If not perfect it will definitely improve your abilities. Well following that principle in mind, I shall embrace this course with open arms and hope to have a great time not only in this course but also in my other courses.