2014年1月30日星期四

First Taste of Recursion

This week in class we looked at recursion over nested lists and recursion for permutations. To me, recursion is completely new and never thought of. The concept of calling a function within the function itself was at first very confusing. However I was able to understand it through tracing the steps one by one, though it took a bit of time.

Not only was recursion new and confusing to me, list comprehensions were also troubling, especially when it is used in combined with recursion. My approach to understanding list comprehensions is through rewriting them into the longer form which I am used to from CSC108. This way takes quite a bit of time and defies the purpose of list comprehensions, so I hope that I can learn to understand list comprehension itself through repeating this approach many times.

Another new thing we learnt was the nameless function lambda. Lambda is a function without a name that is called only once. However most functions of lambda can be accomplished by list comprehensions.

One thing I did not understand from this week was the permutation example given on the CSC148 course website.
The function was:

def perm(s: str) -> {str, ...}:
    return (set(sum([[s[i] + p for p in perm(s[:i] + s[i + 1:])]
                for i in range(len(s))], []))
            if len(s) > 1 else {s})

The main thing I did not understand was the use of sum in the function. Isn't sum only for numbers?

2014年1月22日星期三

SLOG 1 (Object-Oriented Programming?)

These first weeks of CSC148 has been weeks of adjusting, understanding and growth. Both lectures and labs required adjusting to a new learning style compared to CSC108 in the previous semester. This is by learning and understanding new knowledge and concepts, and to change an grow to fit the teaching style of CSC148.

The first difference from before was object-oriented programming. In CSC108, I was used to programming by writing function by function by function. However in lectures of CSC148, programming is based around classes, whcih was new and interesting for me. Compared to just a list of functions, classes seemed more organized, catagorised, and structured.

The second difference was labs.In CSC108, labs were optional and we just had to complete lab exercises online. In CSC148, we now have to attend labs and complete exercises with a lab partner. This was hard for me, because programming with someone else was a lot different from programming alone. When being the navigator, I often felt the urge of getting hold of the keyboard and typing myself. Also I was aware that if I think and work at a pace faster than my partner, she might not be able to think through it herself. As according to my friend in another tutorial section, she doesn't learn a lot from the tutorials because her partner always solves the problem before she can fully think through it. So I stopped myself from over contributing. However because of this, the progress of completing the exercises in lab01 was a lot slower than it should have been, and was not fully completed. So in lab02 I didn't restrict myself from contributing and the exercise was completed within an hour, although I still don't know if this is good or not. However I have still learn't a lot by taking lab01 home and correcting and debugging it. Through debugging lab01, I learnt some thing new. Which was that dictionaries cannot change size during iterations. Which I did not know before.

Another interesting thing that happened this week was learning the ADTs stacks and queues in lab02. In particular, analysing the runtime of stacks and queues of different sizes. It resulted in that the runtime of stacks don't really grow with the size of the stack. However queues grow lineraly with the size of the queue. At first the concepts were abstract and hard to understand, but then I pictured a stack of paper versus a line-up at Starbucks. When adding paper and removing paper from the stack, the stack remains still and unmoved. However the line-up at Starbucks is constantly moving as people enter and leave the line. This gave me an understanding of the queue runtimes. Afterwards, when we were asked to think of more efficient ways of implementing queues, I tried thinking of how I could have a line-up at Starbucks so that the people in line doesn't have to move. Then I realized the only way is to have the cashier moving. But if the cashier keeps moving on, the counter will have to be infinetely long. Then the TA introduced us a new concept of a circular queue, which then I pictured Starbucks counter being a circle with a cashier that moved around inside the circle. Then the people will not need to move. But this is not efficient in the real world, because the cashier will be very tired, and its not that big a deal to move along with a queue. From this it could be seen that not all aspects of programming can be made into an analogy in real life.