The function Danny led us through in class provide me with enough inspiration to finish A1. It's a great idea which i could not have though of myself.
I find the lingoes used to describe the relations among value, variable, memory address in 148 different from those in 108. But since computer science is not about the accuracy of language. I would probably let it go. Still, my understand is a variable contain value, which refers to an memory address as traced in visualizer.
About the scope in python, the key point to scope given in class is to follow the hierarchy system and apply the sequence. For example, in the example given in class, the most important part to understand is how do_global() failed to let print("After global assignment:", spam) print global spam, but instead, nonlocal spam. Since nonlocal already
But looking back, as i am reviewing everything and do the labs all over again, recursion begins to make sense to me. Practice makes perfect. Here are some typical recursion question, one of which i copy from slides, one i think of my self:
largest number in a nested list: the insight is to replace every nested list with the max. and the base case is where there's no nested case: max(l), so the code is return max([largest(i) if isinstance(i, list) else i for i in l)]), by creating a new simple, not nested list
the second example is to decide whether a number is in a nested list. The base case is where there's no nested list, simply n in l. The insight is to n is either in non nested list or inside nested list. Code: return any(n==i if not isinstance(i, list) else whether(n, i) for i in l)
No comments:
Post a Comment