Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #4193
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Fibonacci series recursion error |
| Date | 2011-04-30 10:14 +0200 |
| Organization | A newly installed InterNetNews server |
| Message-ID | <ipggdo$741$1@r03.glglgl.eu> (permalink) |
| References | (1 earlier) <KtMup.29280$8U5.7734@newsfe20.iad> <1zMup.12761$rB2.10164@newsfe21.iad> <b9Nup.59945$yp3.50640@newsfe09.iad> <mailman.1012.1304144107.9059.python-list@python.org> <%4Pup.21891$vC5.6549@newsfe01.iad> |
Am 30.04.2011 09:43 schrieb harrismh777:
> On the other hand, I am very much interested in "yield," because of its
> obvious persistent state, On the other hand, I don't like you fib()
> function because it does not encapsulate the fib generator. I suppose
> you are considering whatever module is holding the fib() code as the
> encapsulation, but I thought the idea from the OP was to create a
> generator that could be called and return a list... this is a little goofy:
> list(islice(fib(), X))
>
> when what was wanted is:
>
> list = fib(X)
You can have both with the following:
def fib(max=None):
a = b = 1
while max is None or a <= max:
yield a
a, b = b, a+b
from itertools import islice
flist = list(islice(fib(), 100000))
flist2 = list(fib(100000))
or - if even the latter is unwanted -
def fibgen(max=None):
a = b = 1
while max is None or a <= max:
yield a
a, b = b, a+b
def fib(max=None, num=None):
if num is None:
return list(fibgen(max=max))
else:
from itertools import islice
return list(islice(fib(max=max), num))
Thomas
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
Re: Fibonacci series recursion error harrismh777 <harrismh777@charter.net> - 2011-04-30 02:43 -0500
Re: Fibonacci series recursion error Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-30 10:14 +0200
Re: Fibonacci series recursion error harrismh777 <harrismh777@charter.net> - 2011-04-30 15:41 -0500
Re: Fibonacci series recursion error Peter Otten <__peter__@web.de> - 2011-04-30 18:07 +0200
Re: Fibonacci series recursion error harrismh777 <harrismh777@charter.net> - 2011-04-30 15:51 -0500
csiph-web