简体   繁体   中英

Python Script is crashing and I don't know why

So I have 2 python programs that determine Fibonacci numbers. One uses memoinization and the other does not. Both work, and are able to determine the numbers when given an input.

However, I need to time them, which is where the 3rd program comes into play. I have it timing both functions for the 1st fib number to the 30th. When I run the 3rd program though, it just seems to crap out when it is calculating the 9th number, and I don't know why.

Can someone help me here? I have the code for all 3 programs below (and the error)

Thanks


Program 1

#!/usr/bin/python

import sys

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

numIn = int(sys.argv[1])
result = fib(numIn)

print 'The %dth fibonacci number is %d' % (numIn, result)

Program 2:

#!/usr/bin/python

import sys

cache = [None]*100

def fib(n):

    if n in cache:
        return cache[n]

    else:
        if n < 2:
            cache[n] = n
        else:
            cache[n] = fib(n-1) + fib(n-2)

        return cache[n]

numIn = int(sys.argv[1])
result = fib(numIn)

print 'The %dth fibonacci number is %d' % (numIn, result)

Program 3:

#!/usr/bin/python

import sys,timeit,time

for i in range(1, 30):
        t1 = timeit.Timer('fib(%s)' % i, 'from problem1 import fib').timeit()
        t2 = timeit.Timer('fib(%s)' % i, 'from problem2 import fib').timeit()
        print 'The time for the %d th number is %f (Problem 1 - No Memo' % (i,t1)
        print 'The time for the %d th number is %f (Problem 1 - Memo' % (i,t2)

ERROR:

The 5th fibonacci number is 5
The 5th fibonacci number is 5
The time for the 1 th number is 0.215671 (Problem 1 - No Memo
The time for the 1 th number is 0.247929 (Problem 1 - Memo
The time for the 2 th number is 0.606024 (Problem 1 - No Memo
The time for the 2 th number is 0.269888 (Problem 1 - Memo
The time for the 3 th number is 1.027372 (Problem 1 - No Memo
The time for the 3 th number is 0.298666 (Problem 1 - Memo
The time for the 4 th number is 1.839900 (Problem 1 - No Memo
The time for the 4 th number is 4.466314 (Problem 1 - Memo
The time for the 5 th number is 3.117439 (Problem 1 - No Memo
The time for the 5 th number is 0.308327 (Problem 1 - Memo
The time for the 6 th number is 5.178429 (Problem 1 - No Memo
The time for the 6 th number is 8.496127 (Problem 1 - Memo
The time for the 7 th number is 8.486079 (Problem 1 - No Memo
The time for the 7 th number is 12.532179 (Problem 1 - Memo
The time for the 8 th number is 13.571108 (Problem 1 - No Memo
The time for the 8 th number is 0.321315 (Problem 1 - Memo
Traceback (most recent call last):
  File "./problem3.py", line 7, in <module>
    t2 = timeit.Timer('fib(%s)' % i, 'from problem2 import fib').timeit()
  File "/usr/lib/python2.7/timeit.py", line 195, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 6, in inner
  File "problem2.py", line 18, in fib
    cache[n] = fib(n-1) + fib(n-2)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

if n in cache is the wrong logic. Your probably want if cache[n] is not None

n is supposed to represent the nth fibonacci number, so the cache is storing fibonacci numbers, not the indicies of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM