Friday, January 30, 2015

Week 4: Peek at Recursion & What I learnt so far

This week, Danny taught the class about simple recursion. Recursion indicates the use of the function itself to solve something like nested nested list. During the lab, I also learnt about tracing the recursion code where I had to think hard how the nested list would look like as it continuously returned the function. Most importantly, there was a use of " if isinstance(L, list)", which indicates if L is a list, then it passes the requirement. The use of "if isinstance(L, other type)" also works for int, str, and float. For instance, I can write the code as if isinstance(L, str), the requirement to pass this condition is that L has to be a string. Not only that, I had to consider the case when L is non-list by creating else: and return statement.

The below coding is a simple example of recursion with tracing a recursion. It is also an example I practiced during this week's lab.

def count_elements(L):
       ’’’(list or non-list) -> int

       Return 1 if L is a non-list, or number of non-list elements in
       possibly-nested list L.

       Assume: L is any Python object.
       >>> count_element([1,2,3])         #usually two examples are required. 
       3
       >>> count_elements('Hello')   # L is non-list.
       1 
       ’’’

       if isinstance(L, list): # if L is a list
                return sum([count_elements(x) for x in L])
       else: # if L is not a list, count it
                return 1

Example of Tracing using above examples.   Note that return replaces with "-->" sign.

count_elements([1,2,3])
--> sum([count_elements(x) for x in L])  # since L is a list
--> sum([count_elements(1), count_elements(2), count_elements(3)]) # use of recursion
--> sum([1, 1, 1])
--> 3

count_elements('Hello')
--> 1  # since L is not a list, this function simply returns 1.

To review what I learnt this week, I will do more practice on recursion problems. Furthermore, for upcoming term test 1, I need to go over extra questions provided by professor for the lab. For first few weeks, I got to learn various things about coding including class inheritance, subclass, and data abstract. The most difficult part I found during the first few weeks is the class inheritance using a subclass. At first, I didn't understand why I have to use raise NotImplementedError("subclass needed"), However, now with assignment 1 and many practices during a lab, I was able to get how the class inheritance can be useful for a programmer. The most interesting study I found so far is the use of recursion how it is able to call the function itself to analyze the nested list. I hope I can learn more things about coding through CSC148.

Overall, I think that enough practices and effort make this course a lot easier and enjoyable. I hope someone in the future who will take CSC148 would be able to read my advice. As for other people's SLOG, I read quite a few of them, and most of SLOG entries were finely done and interesting. So I think later on I will probably post some of SLOG links which I find useful and interesting.




Friday, January 23, 2015

Week 3: Why Geeks need to write?

WHY Geeks need to write? Well there is a simple answer to this. Even computer scientists need to be able to express and to communicate their idea and coding. Moreover, many computer language methods are in relation to the basic English skill. It is a good habit for a person to keep a record of what they are trying to do which helps geek in terms of their thinking skills and enhance their insight. Whether, he or she is either scientist, architect, artist, writer or actor, writing and communication skills are one top priority for a person to succeed on their job. I consistently write my slog even during the last term for my csc165 class. When I reflected upon what I learnt each week, I am able to get a track of what I need to learn for my test and assignment. Moreover, it is really interesting when I look at other people's SLOG.

Anyway, this week, I was really busy with working with assignment 1 where I need to make game using subtract square strategy.. Although my partner and I did our best to complete the assignment, there was some parts which we were not familiar of. Thankfully, we went to Danny's working office hour and Computer science help center to get an idea on how we actually need to approach effectively to make our code working.

This week, I learnt about how I can use "raise NotImplementedError('Subclass needed'). Moreover, I learnt about class inheritance and stuff about parent class and child class. Although I am not good at using class, I will start reviewing on this material to enhance my skill.  I hope I can have a great time learning coding at CSC148.