where the function has problem? n = 900 is OK , but n = 1000 is ERROR
Ulrich Eckhardt
ulrich.eckhardt at dominolaser.com
Mon Aug 1 06:50:24 EDT 2011
Steven D'Aprano wrote:
> jc wrote:
>> n = 900
>> cache = range(0 , n + 1 , 1)
>> for i in cache:
>> cache[i] = -1
>
> This is a waste of time. Better to write:
>
> cache = [-1]*900
Since he's computing the Fibonacci number of n, and n is 900, he needs
cache = [-1] * (n + 1)
;^)
> Even better is to use a dict instead of a list.
Simpler, yes. Better for a beginner, of course. Better, maybe not. The use
case here will cause the cache to be used from index 1 upwards, so using an
array to store the element is faster and smaller. I'd consider resizing the
cache dynamically though.
Cheers!
Uli
--
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
More information about the Python-list
mailing list