Friday, February 14, 2014

Week 4&5: Recursion...

Before I switched to CS, I was warned by a friend that dropped out of CS on his third year. He told me the thinking process to derive a code gradually becomes unfathomable and abstract. It would be more poetic to picture a battlefield, a scene where your elder comrade had fallen right before your eyes, warning you to take heed before you venture far into the chaos... But I digress. I took his two cents and was mentally prepared to face difficult concepts.

So here I was, dealing with recursion for seemingly the first time of my life. Both Dan and our TA expressed the importance and delicacy of recursion. I get it. It's quite peculiarly logical, but hard to grasp because of you're "defining something in terms of itself". It's no longer a one-shot logic and I feel like I'm getting to the weird part of math. Anyhow, Dan did a wonderful job showing us the ropes: tracing from the simplest parameter. I watched the Recursion lecture by Harvard University on Teaching Tree, but that didn't help me any better than Dan's lecture. Thus far, for every single recursion code CSC148 showed us, trying to trace it in one go is sometimes possible, but less effective than doing an organized, step-by-step tracing method.

For example, from the Permutations and String Length Examples shown in lecture,

def strlen(s):
  '''(str) -> int
  Return length of s.

  >>> strlen('abcd')
  4
  '''
  if s == '':
    return 0
  len_smaller = strlen(s[1:])
  return len_smaller + 1

You can trace it starting with strlen(''), the simplest case. 
Then, trace strlen('a') with the knowledge that strlen('') returns 0. 
So on with strlen('ab') then strlen('abc'). 

I can mentally trace it without plugging in values in parameters for this one by considering how recursion stops when you're left with an empty string after eliminating the first index. In other words, the very first index of a non-empty string will always be counted by the + 1 in return len_smaller + 1, then consider how strlen(s[1:]) rids the first character in the string each time and basically adds one for each character because of the last line of the code.

The latter method of tracing is way clumsier while in the end, I still indirectly traced it considering the base case strlen('') and strlen('a'). I'm assuming that for recursive codes, it'd be more efficient to be organized and start from bottom to top or top to bottom depending on the case.

Another example is the gcd code from the recursion lab. This is easier done from top to bottom when you are given csc_gcd(15,35) only.

def csc_gcd(n1: int, n2: int) -> int:
"""Return the GCD of non-negative integers n1 and n2
>>> csc_gcd(0, 0)
0
>>> csc_gcd(5, 0)
5
>>> csc_gcd(15, 35)
5
"""
# The gcd of n1 and n2 is n1 if n2 is zero, otherwise it is
# the same as the gcd of n2 and (n1 % n2)
if n2 > 0:
return csc_gcd(n2, n1 % n2)
else:
return n1















So you start from gcd(35,15) and then do csc_gcd(n2, n1 % n2) over and over again in an organized manner. This table was introduced to us by our TA Bryan. He also told us it would come in very handy during tests and exams because it is easy to help us trace, it is easy for the marker to give you full marks as well.

My bigger issue is with making a code using recursion. Honestly, I haven't gotten much practice yet. The labs felt rushed because I don't like missing anything the TA says, in case it is important like the table (that comes in really handy imo). So when I spend time to listen to the TA, I am left with less time to read the next section and actually write a code for it. He tries to compensate the lack of time by taking up codes (solutions) early, which means I never really got to think about it much before time's up. After the labs, I feel that I should redo the lab at my own time. Yet I didn't. At least not yet.

Honestly, this week had been quite hectic with family and friends. Too much trivial but time consuming things stopping me from studying and finishing up my assignments. So, hopefully I will get the time to think about binary_search(lst, s) and read the solution this coming weekend, as well as finish my A1.

Last but not least, I read two blogs today:
http://ilovecsc148.wordpress.com/ - 1/2/2014 Recursion with a bit of Intuition
http://quererna.blogspot.ca/ - 23/1/2014 Some comprehending for the introduction of Object-oriented Programming
I like the former better because it's not that easy to read the second one. I like the approach quererna has. Also, ilovecsc148 made a well-detailed tutorial and a nice description of his/her feeling towards recursion. Nice job to both of them! :)

1 comment:

  1. This is one of the better-written blogs. Thanks for another reminder of how this all works. :)

    ReplyDelete