[cse491] CSE 491 Homework 1

C. Titus Brown ctb at msu.edu
Tue Sep 2 12:13:21 PDT 2008


On Tue, Sep 02, 2008 at 03:02:33PM -0400, Maurice Wong wrote:
-> FibonacciIterator(n)
-> ReentrantFibonacciIterator(n)
-> Fibonacci_generator(n)
-> 
-> should all return a list of the first n elements in the Fibonacci sequence,
-> correct?

Yep!  However, they should return it in the appropriate form (as an
iterator or a generator) rather than as an explicit list.  The tests do
a type check on the generator to make sure it *is* a generator but
there's no simple way for me to do that for the iterators.  Basically
they should be implemented as classes defining an __iter__ method.

-> Also, can you repeat what you meant when you said about the
-> 
-> x = homework1.FibonacciIterator(8)
-> assert list(x) == fib8
-> assert list(x) == []

I can't remember exactly what I said, but I think for many people this
is a non-intuitive result: first x is one thing, and then x is another
-- how is that possible?

The answer is that 'list(x)' is actually *doing* something to x -- it's
consuming values produced by iterating over x -- and when the 'x'
iterator has been consumed once, it no longer produces any values.
Compare to the re-entrant iterator which produces a new iterator object
every time you iterate over it.

cheers,
--titus
-- 
C. Titus Brown, ctb at msu.edu



More information about the cse491-fall-2008 mailing list