Beispiel #1
0
def testFib1():
    a = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
    for i in range(0, 10):
        if (a[i] != fib1(i)):
            print('Error in function fib1 ')
            return
    print('fib1 passed test from 0 to 10')
Beispiel #2
0
            return
    print('fib1 passed test from 0 to 10')


#test for function 2 polynomial algorithm
def testFib2():
    a = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
    for i in range(0, 10):
        if (a[i] != fib2(i)):
            print('Error in function fib2 at index {i}')
            return
    print('fib2 passed test from 0 to 10')


#test that the functions are working properly
testFib1()
testFib2()

#each functions gives the requested fibonacci number and records the time it takes
n = int(input('Enter an integer for n: '))

start_time = time.time()
answer = fib1(n)
end_time = time.time() - start_time
print("fib1 answer: {} with time: {} seconds ---".format(answer, end_time))

start_time = time.time()
answer = fib2(n)
end_time = time.time() - start_time
print("fib2 answer: {} with time: {} seconds ---".format(answer, end_time))
 def test_fib1(self):
     with self.assertRaises(RecursionError):
         print(fib1(5))
Beispiel #4
0
Purpose: Create a graph of the runtimes of each algorithm

"""
from fibonacci import fib1
from fibonacci import fib2
import matplotlib.pyplot as plt
import numpy as np
import time

plt.xlabel('n')
plt.ylabel('Time')
plt.title('Running time of two fibonacci algorithms')

x = [0,1,5,10,15,20,25,30,35]
#values taken from plugging in x values into fibonacciTest and multiplying by 10^5
expY = [0]
polyY = [0] 
for i in range(0,8):
	start_time = time.time()
	fib1(x[i])
	expY.append(time.time() - start_time)
	start_time = time.time()
	fib2(x[i])
	polyY.append(time.time() - start_time)

plt.plot(x, expY, label = "Exponential(fib1)")
plt.plot(x, polyY, label = "Polynomial(fib2)")

plt.legend()
plt.show()
Beispiel #5
0
# Modules dans python

# De ce dossier, on a un fichier fibonacci.py

import fibonacci

# On peut alors accéder aux fonctions du module (fib1 et fib2)
fibonacci.fib1(5)

# Importation d'une fonction
from fibonacci import fib2

# Exécution en mode interactif
%run fibonacci # Exécute le main du module
fibonacci.__name__ # Renvoie "fibonacci" car ce n'est pas le main

# Rechargement du module en séance interactive
import importlib
importlib.reload(fibonacci)

# Path
# Regarde dans sys.path et dans le répertoire courant
import sys
sys.path
# peut être dans $HOME/.local/lib/python3.6/sites-packages
# juste besoin de déplacer le .py dans ce répertoire
# Le module déjà importé est builtins et contient toutes les fonctions de base

# Module os
# Permet interfaçage avec l'OS, alternative aux script shell
Beispiel #6
0
Out[3]: module

dir(fibonacci)
Out[4]:
['__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'fib1',
 'fib2']

fibonacci.fib1(10)
Out[5]: [1, 1, 2, 3, 5, 8]

#OU
from fibonacci import fib1, fib2

fib1(10)

"""PACKAGES : collection logique de modules (dossier) (cf www.python.org)
Toujours avoir un fichier __init__.py, même s'il est vide !!!"""


"""CLASSES"""
class MyClass:
    myPi = 3.17
Beispiel #7
0
 def test_fibonacci(self):
     self.assertEqual(fib1(1), 1)
     self.assertEqual(fib1(3), 2)