Learning how to trace recursion in CSC 148 was an amazing experience. Although I had very little knowledge about it, Professor Heap taught us in details which helped my understanding. I also had a quiz on tracing recursion last week.
I would like to demonstrate my skills on tracing recursion. I will use the function count_elements that was posted on CSC 148 lab 3 made by professors. I will try to solve the first four count_elements questions that are posted as well.
(Lab 3 Source: http://www.cdf.toronto.edu/~csc148h/winter/Labs/lab03/handout.pdf).
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.
"""
if isinstance(L, list):
return sum([count_elements(x) for x in L])
else:
return 1
(Source: http://www.cdf.toronto.edu/~csc148h/winter/Labs/lab03/handout.pdf).
1. count_elements('snork') -> 1 # since 'snork' is a non-list
2. count_elements([2, 1, 3]) -> sum([count_elements(x) for x in [2, 1, 3])
-> sum([1, 1, 1]) # already traced count_elements(int)
-> 1+ 1 + 1
-> 3
3. count_elements([2, 1, 3, '[4]']) -> sum([count_elements(x) for x in [2, 1, 3, '[4]'])
-> sum([1, 1, 1, 1]) # already traced count_elements(int) and # count_elements(str) before
-> 1 + 1 + 1 + 1
-> 4
4. count_elements([7, [2, 1, 3], 9, [3, 2, 4]]) -> sum([count_elements(x) for x in [7, [2, 1, 3], 9, [3, 2, 4]])
-> sum([1, 3, 1, 3]) # already traced count_elements(list of # int) before
-> 8
(Source: http://www.cdf.toronto.edu/~csc148h/winter/Labs/lab03/handout.pdf).
This week, I learnt how to write codes for unit tests. However, I was not able to review the materials covered on Monday as I had my first CSC 148 term test on Wednesday. So I mainly focused on studying the previous course materials such as the basics of Object-Oriented Programming. I hope I can achieve high marks for this term test. Right after I finished my term test, I studied about unit tests for two hours. Although I had some trouble understanding the material, I managed to learn the course materials well. Although both queue and stack are basically lists, how we remove and return the removed elements are different. Stack is LIFO(Last In First Out) whereas Queue is FIFO(First In First Out). I realized how we can use lists to create new class like Stack and Queue. Writing codes for queue_list and stack_list was very easy for me as I already reviewed the materials. Although I have encountered unit tests in CSC 108, this was my first time writing codes for unit tests. I really love CSC 148 course. It always teaches me various things about programming.
No comments:
Post a Comment