[cse491] HW1: Reentrant Iterators

Edward Waller wallered at msu.edu
Thu Aug 28 20:02:33 PDT 2008


On Aug 28, 2008, at 10:19 PM, C. Titus Brown wrote:

> On Thu, Aug 28, 2008 at 10:13:48PM -0400, Edward Waller wrote:
> -> So from wikipedia I see that
> ->
> -> Reentrant: A computer program or routine is described as  
> reentrant if
> -> it can be safely executed concurrently; that is, the routine can  
> be re-
> -> entered while it is already running. To be reentrant, a function  
> must:
> ->
> -> Hold no static (global) non-constant data.
> -> Must not return the address to static (global) non-constant data.
> -> Must work only on the data provided to it by the caller.
> -> Must not rely on locks to singleton resources.
> -> Must not call non-reentrant functions.
> ->
> -> Since def next(self): takes no arguments, how exactly are we  
> supposed
> -> to make this reentrant?  The only thing I can think of is making  
> a new
> -> instance for the iterator like:
> ->
> -> class ReentrantFibonacciIterator(FibonacciIterator, object):
> ->    def __iter__(self):
> ->      return ReentrantFibonacciIterator(self.n)
> ->
> -> But i'm not sure if i'm the right track with that, it doesn't seem
> -> like you're testing this?
> ->
> -> Anyway if anyone has any ideas feel free to share =].
>
> Hi Edward,
>
> you're on the right track.  The key tests are these, of course:
>
> ---
> x = homework1.FibonacciIterator(8)
> assert list(x) == fib8
> assert list(x) == []
>
> x = homework1.ReentrantFibonacciIterator(8)
> assert list(x) == list(x)
> assert list(x) == fib8
> ---
>
> An equivalent way to state things is this: when I do
>
> x = homework1.FibonacciIterator(8)
> for a in x:
>  for b in x:
>     print a, b
>
> I would expect to see
>
> 0 0
> 0 1
> 0 1
> 0 2
> 0 3
> 0 5
> ...
>
> for a re-entrant iterator, and
>
> 0 1
> 0 2
> 0 3
> 0 5
> 0 8
> ...
>
> for a non-re-entrant one.
>
> And note that 'next(self)' does indeed take *one* argument...  And  
> that
> there's no requirement that __iter__ return self.
>
> OK, enough hints.  Go make it work :)
>
> cheers,
> --titus
> -- 
> C. Titus Brown, ctb at msu.edu
>

Okay, since the sequence is [0, 1, 1, 2, 3, 5, 8, 13, 21], shouldn't  
that code:

> x = homework1.FibonacciIterator(8)
> for a in x:
>  for b in x:
>     print a, b

store 'a' as the first number in the sequence, then continue with b  
for the 2nd to last so you get:

0 1
0 1
0 2
0 3
0 5
0 8
0 13
0 21

with two 1s to start?

Anyway, the reason I asked about the tests was because the way you  
worded it

"ReentrantFibonacciIterator(n) - a re-entrant version of the fib  
iterator,
     i.e. something that can be iterated over multiple times."

and the way you tested it made it seem like it was just an iterator  
that reset itself when it reached the end.  But thanks for the  
clarification =].
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.idyll.org/pipermail/cse491-fall-2008/attachments/20080828/8b719c90/attachment.html 


More information about the cse491-fall-2008 mailing list