Dictionary project
Ben C
spamspam at spam.eggs
Sat Mar 11 20:03:18 EST 2006
> So here's a different approach, which I think does meet the spec:
>
> from itertools import tee
> def allwords2(alphabet="abcd", maxlen = 4):
> def wordgen():
> for char in alphabet:
> yield char
> for partial in allwordstee[1]:
> if len(partial) == maxlen:
> raise StopIteration
> for char in alphabet:
> yield partial+char
> #tee creates two copies of the iterator:
> # one is returned to the caller
> # the other is fed back to the generator
> allwordstee = tee(wordgen())
> return allwordstee[0]
Very neat. I never knew about tee.
More information about the Python-list
mailing list