Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87860

Re: fibonacci series what Iam is missing ?

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'cache': 0.07; 'subject:missing': 0.07; 'append': 0.09; 'augmented': 0.09; 'caching,': 0.09; 'converted': 0.09; 'matched': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'jan': 0.12; "wouldn't": 0.14; "'''": 0.16; 'arg': 0.16; 'assignment.': 0.16; 'confuse': 0.16; 'count,': 0.16; 'expression,': 0.16; 'happy.': 0.16; 'iteration': 0.16; 'iteration,': 0.16; 'mutable': 0.16; 'op.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'recurrences': 0.16; 'reedy': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; 'value.': 0.19; 'work,': 0.20; 'header:User-Agent:1': 0.23; 'requirement.': 0.24; 'initial': 0.24; 'posts': 0.26; 'least': 0.26; 'skip:_ 20': 0.27; 'values': 0.27; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'needed.': 0.30; 'code': 0.31; 'too.': 0.31; 'initialized': 0.31; 'subject:what': 0.31; 'probably': 0.32; 'figure': 0.32; 'skip:_ 10': 0.34; 'but': 0.35; "didn't": 0.36; 'should': 0.36; 'list': 0.37; 'easily': 0.37; 'depends': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'read': 0.60; 'dave': 0.60; 'length': 0.61; 'full': 0.61; 'new': 0.61; 'term': 0.63; 'maximum': 0.63; 'more': 0.64; 'statement,': 0.68; 'default': 0.69; 'faster.': 0.84; "it'd": 0.84; 'min': 0.84; 'received:fios.verizon.net': 0.84; 'recursion,': 0.84; 'yours': 0.88; 'angel': 0.91; 'hand,': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: fibonacci series what Iam is missing ?
Date Mon, 23 Mar 2015 21:45:25 -0400
References <CACT3xuVF-MOVQK5wSorRSAfpAQEXvS7QsqarACVZKXsQ0bxzpQ@mail.gmail.com> <CAPTjJmrRv+DQGpzdY5te-vk_u9p5AzWYQ-6B=m1knPoaXpRmRQ@mail.gmail.com> <55105EF4.2070805@davea.name> <meq5i2$53s$1@ger.gmane.org> <55109DEF.9070001@davea.name>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-98-114-97-173.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0
In-Reply-To <55109DEF.9070001@davea.name>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.91.1427161553.10327.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1427161553 news.xs4all.nl 2830 [2001:888:2000:d::a6]:41739
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:87860

Show key headers only | View raw


On 3/23/2015 7:12 PM, Dave Angel wrote:
> On 03/23/2015 06:53 PM, Terry Reedy wrote:

>> Iteration with caching, using a mutable default arg to keep the cache
>> private and the function self-contained.  This should be faster.
>>
>> def fib(n, _cache=[0,1]):
>>      '''Return fibonacci(n).
>>
>>      _cache is initialized with base values and augmented as needed.
>>      '''
>>      for k in range(len(_cache), n+1):
>>          _cache.append(_cache[k-2] + _cache[k-1])
>>      return _cache[n]
>>
>> print(fib(1), fib(3), fib(6), fib(5))
>> # 1 2 8 5
>>
>> Either way, the pattern works with any matched pair of base value list
>> and recurrence relation where f(n), n a count, depends on one or more
>> f(k), k < n.  'Matched' means that the base value list is as least as
>> long as the maximum value of n - k.  For fib, the length and max are
>> both 2.
>>
>
> I almost used a default value as a cache, but didn't want to confuse the
> OP.  Also, your present code does not use recursion, so probably
> wouldn't make the prof happy.

I did not read the initial posts with that requirement.  Iteration is 
easily converted to tail recursion, which is definitely the way to 
calculate recurrences with more than one term without a full cache.

> On the other hand, my loop makes some non-obvious assumptions, like that
> append is always the right place to put a new value.  I knew it'd  work,
> since fib calls itself with n-1.  But it wouldn't directly work if the
> function had recursed on n-2 and n-3.  Yours would.
>
> I prefer iteration, but I still figure this is an assignment.

def fib(n, _cache=[0,1]):
     '''Return fibonacci(n).

     _cache is initialized with base values and augmented as needed.
     '''
     k = len(_cache)  # min n without a value in cache
     if k <= n:
         _cache.append(_cache[k-2] + _cache[k-1])
         return fib(n)
     else:
         return _cache[n]

print(fib(1), fib(3), fib(6), fib(5))
# 1 2 8 5

If one does not like the append as a statement, and prefer it as part of 
the return expression, this works too.

def fib(n, _dummy=None, _cache=[0,1]):
     k = len(_cache)
     return (fib(n, _cache.append(_cache[k-2] + _cache[k-1]), _cache)
                if k <= n else _cache[n])

However, I am not a fan of puritanical functionalism.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: fibonacci series what Iam is missing ? Terry Reedy <tjreedy@udel.edu> - 2015-03-23 21:45 -0400

csiph-web