Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

in course 6.00, unit 1 practice test, problem 7. I thought it would return a list of the "non-list" elements of the input, instead it returns a list of the elements that don't contain lists. Is there an easy explanation why? I thought the results object would keep adding on items, Thank you

OpenStudy (anonymous):

I should have included the code: def f(L): result = [] for e in L: if type(e) != list: result.append(e) else: return f(e) return result Problem 7.2 What does this print? print f([1, [[2, 'a'], ['a','b']], (3, 4)])

OpenStudy (anonymous):

Hi jagoji. When you don't know what's going on, write down what's happening step by step with an easy example. Let's say our list was L = [1, [0]]. The first element is an integer, the second element is a list containing the integer 0. This is what happens when f(L) is called: result = [] e = 1 result.append(e) (because type(e) != list) e = [0] return f(e) (because type(e) == list) Now we know what the function will return is f(e) == f([0]), so let's examine what happens in f([0]): result = [] e = 0 result.append(0) (because type(e) != list, result is now [0]) now we are done iterating over the elements of [0] return result => returns [0] In plain English, this is what happens: When you give the function a list which does not contain another list, it returns the original list. Else, when the original list contains a list, it calls that function on that list. If this list contains another list, the process continues. Ultimately, the function will return the elements of the first list it encounters which does not contain another list.

OpenStudy (anonymous):

Here's a similar function, try to figure out what it does. def mystery(lst): result = [] for x in lst: if type(x) != list: result.append(x) else: for y in mystery(x): result.append(y) return result

OpenStudy (anonymous):

Thank you dust7, Two things happen in the first code that still don't quite make sense. In the first call of f(), it steps through each element of the sequence, but stops at the first instance of a list not containing another list, the final tuple (3,4) never gets picked up by the for loop. Why didn't that for loop ever finish? Second is a question of scopes. I thought that functions called by another function could "see" the variables in the function that called them. Meaning that I thought when when f() is called in recursion, the result list object was the same, and that it would append to it, not over-write it.

OpenStudy (anonymous):

>> the final tuple (3,4) never gets picked up by the for loop. Why didn't that for loop ever finish? A function ends when it reaches a return statement. e never gets to (3, 4) because the 'else' branch of the loop which has a return statement is reached beforehand. Here's a simple example which is the same conceptually: def f(): result = [] for I in range(100): if I < 5: result.append(I) else: return result return result a = f() a == [0, 1, 2, 3, 4] => the function terminates, returning result, when I == 5. The second return statement is never reached. >> I thought when when f() is called in recursion, the result list object was the same, and that it would append to it, not over-write it. Remember that the first thing that happens on each call is setting result = []. Every call starts out with a fresh result list which is initially empty.

OpenStudy (anonymous):

Thank you again dust7, been walking through the steps again and realized that result was re-initialized each call. I see now that the return statement cancels calculation and returns a value at that point. You're very patient.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!