This week, I learnt about the unittest and testcase. Sadly, there was only one lecture this week due to the term test 1 taking a place. I didn't have any problem with unittest and how I need to use self.assertTrue( ), self.assertFalse( ), and self.assertEqual( ) method. Many things about unittest was already taught in CSC108 class, so I thought this week was a brief review week and preparation time for the term test 1. Personally, I thought the term test 1 went well, but I am certain that this does not mean I can slack off for the rest of the course. As this is only a beginning, I will do my best to review any new materials I am learning through this course.
Since I also did review recursion for my term test 1. I just want to go over it continuing from the last week's SLOG entry. Last week, I mainly talked about the simplest recursion example. This week, I want to go over the recursion example in the lab which I really had a trouble during the recursion lab. The example is as below.
def length(L):
""" (list or non-list) -> int
Return the max length of the list (possibly nested list) in L.
Pre-condition: L should be Python object either list or non-list.
>>> length([1,2,3])
3
>>> length([1,[2,3,4]])
3
>>> length(12)
0
"""
if isinstance(L, list):
return max([len(L)] + [length(x) for x in L])
else: # if L is not a list.
return 0
Trace length([1,2,3])
--> max([len([1,2,3])] + [length(x) for x in [1,2,3]]) # since L is a list.
--> max([3] + [0, 0, 0])
--> 3
Trace length([1, [2,3,4]])
--> max([len([1,[2,3,4]])] + [length(x) for x in [1,[2,3,4]]]) # since L is a list.
--> max([2] + [3])
--> max([2, 3])
--> 3
At first I was wondering how I can do the comparison of the length of two lists through recursion. Thankfully, TA told me to read the condition carefully which was given on the lab handout. Also instead of using list append method, I had to use + in order for me to create an instance where I can find the maximum length of the list among two lists given. Not only that, I was amused by how I can even find the maximum and minimum length of nested list in L.
What I thought difficult this week was use of stack and queue method in the lab. However, the most difficulty I had experienced was due to snow storm which made me totally exhausted. What I felt easy during this week's lecture was on the unittest, how I can import unittest and using unittest, I can use unittest.TestCase to evaluate at a certain given example whether the function within the class is working properly.
During this week's lab, sometimes lab material is hard to solve just by looking once. So I always think that it is best for one to look at the lab handout several times before the tutorial and actually try to solve the problem by yourself instead of asking TA that you just don't get it. I think just spending an hour for the preparation of the lab before the tutorial is really helpful and let you do well on the quiz.
No comments:
Post a Comment