This week I think I have to look back and see if I agree
with my early ideas. I visited SLOG 6,and found that
my comprehension on the depth of a node was totally wrong. It should be counted
from the root to the node but not from the node to the leaf. Also, I visited
SLOG3, and I think I have to supplement something on it. In SLOG3 I talked
about the concept of Recursion, but I didn’t mention the way to write a
recursion. There are quite a lot of ways to write recursion, and we have to
decide which one to use. I can just make some examples to lead.
Suppose we have a nested list [3,[5,6,7],[2,[1]],2], and we
want to sum the data inside. By calculating we can easily get the answer, but it
is not working for a computer. So we need to write codes by using recursion. One
of the skills to write a correct recursion is to find the final thing we
operate. In this example, we can tell that those numbers are the so-called
final things. So we start the code like this:
def nested_list(L):
if
isinstance(L,int):
return L
Then we have to consider about the core of the problem:
the way to call the function itself.
If we call [nested_list(x) for x in L] for just once, it
means we check if 3,[5,6,7],[2, [1]], 2 are int. If true, then put it into a blank
list. If we call it twice, then 3,5,6,7,2,2 are in the
list, and there will only be a list [1] still not there. Call it again, we have
all the things in the list. All we have to do here is to sum the list. So we
can write a code like this:
return sum ([nested_list(x) for x in L]).
To make it clearer, I put the codes together:
def nested_list(L):
if
isinstance(L,int):
return L
else:
return sum ([nested_list(x) for x in L])
I also read the slogs written by some of my classmates,
and their idea helps me a lot. Plenty of my classmates are puzzled on testing
as well, and I found that we all struggled about the Minimax of the
assignment2. Object Oriented Design is not a common problem, since it is the
base of CSC148. But I think the easier it is, the more careful we should be. The
easiest part is always the part which makes most people careless.
没有评论:
发表评论