def find_num(SL: 'SearchList', n: int) -> bool:
"""
Return True if n is in SL, False otherwise
after a short discussion with my partners, we quickly formed a solution. The idea is: "for item in SL", check each item in the list SL, if the item is a number, then check if item == n; if the item is a list, then we check its items one by one, using for item in list. If we find any item equals to n, then return True and stop; if have checked all the items but no one equal to n, return False.
We were happy to have a solution. While my partner was typing the code in Pyphon, suddenly I remembered that the function the instructor showed in class has only one line -- return line for function body while our code has 8 someting lines. I realized what we had done is what we learned in CSC108, not CSC148. Then I asked my partner stop typing and told him that we need to write a one-line body function. Surprised by my words, my partner didn't believe we can do the job by one line. Then we checked the examples teacher used in class.
We checked the example "sum_list(L: list)", it uses only one line to sum up the numbers in a possibly nested list of numbers. We studied the function carefully, and wrote our one-line body function. save, run, and doctect, call check, everything was ok. Then we called TA to check our job, "GOOD!" was his answer.
Using this idea, we quickly completed the second question, and then did some extra problems.
The key is using recursive code; and the power is recursion insight.
No comments:
Post a Comment