def d_fib(n): L = [0,1] if n > 1: i = 2 while i <= n: L.append(L[-1]+L[-2]) i += 1 return L[n]
d_fib(10)
d_fib(100)
def fib(n): if n == 0 or n == 1: return n else: return fib(n-1) + fib(n-2)
We compute the minimum number of packages needed to form n bulbs if we have packages of size 1, 2, 6, and 8.
def bulb(n): L = [0,1,1,2,2,3,1,2,1] if n > 8: i = 9 while i <= n: L.append(min(L[i-8],L[i-6],L[i-2],L[i-1])+1) i += 1 print(L) return L[-1]
bulb(30)
The function paths counts the number of distinct paths from the start to the end of the maze if we can only move down and to the right.
def paths(M): P = [[0 for columns in range(len(M[0]))] for rows in range(len(M))] # case where start or end is blocked if M[0][0] == -1 or M[-1][-1] == -1: return 0 P[0][0] = 1 # first row for j in range(1,len(M[0])): if M[0][j] == -1: P[0][j] = 0 else: P[0][j] = P[0][j-1] # first column for i in range(1,len(M)): if M[i][0] == -1: P[i][0] = 0 else: P[i][0] = P[i-1][0] # all other entries for i in range(1,len(M)): for j in range(1,len(M[0])): if M[i][j] == -1: P[i][j] = 0 else: P[i][j] = P[i-1][j] + P[i][j-1] for row in P: print(row) return P[-1][-1]
maze = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
paths(maze)
def largeSub(word): ma = 1 M = [[1 for j in range(len(word))] for i in range(len(word))] for k in range(len(word)-1): if word[k] != word[k+1]: M[k][k+1] = 0 for k in range(2,len(word)): for i in range(len(word)-k): j = k+i if word[i] == word[j] and M[i+1][j-1]: M[i][j] = 1 ma = j-i+1 else: M[i][j] = 0 for m in M: print(m) return ma
largeSub('aabbaaccaabac')
def pizza(n): M = [2000] T = [2000] for i in range(n): M_old = M[-1] T_old = T[-1] M_new = 0.75*M_old + 0.10*T_old T_new = 0.15*M_old + 0.85*T_old + 50 M.append(M_new) T.append(T_new) return M,T
pizza(0)
pizza(1)
mario, tony = pizza(15)
import matplotlib.pyplot as plt
plt.plot(mario,label="Mario's") plt.plot(tony,label="Tony's") plt.xlabel('Week') plt.ylabel('Pizzas Sold') plt.legend() plt.show()
def eagle(n): j = [60] a = [140] for i in range(n): j_old = j[-1] a_old = a[-1] j_new = 0.05*j_old + 0.65*a_old - 52 a_new = 0.45*j_old + 0.68*a_old + 72 j.append(j_new) a.append(a_new) return j,a
juv, ad = eagle(25)
plt.plot(juv,label="juveniles") plt.plot(ad,label="adults") plt.xlabel('5 year period') plt.ylabel('population') plt.legend() plt.show()
def logistic(n,r): x = [0.5] for i in range(n): x_old = x[-1] x_new = r*x_old*(1-x_old) x.append(x_new) return x
plt.scatter(range(101),logistic(100,3.8)) plt.show()
1.5, 2.3, 2.6, 3.1, 3.3, 3.5, 3.8
import numpy as np import matplotlib.pyplot as plt X = np.linspace(2.7,4.0,500) def logistic(n,r): L = [0.35] for i in range(n): last = L[-1] new = r*last*(1-last) L.append(new) return np.array(L) plt.figure(figsize=(14,10)) for x in X: plt.scatter([x for i in range(100)],logistic(1000,x)[-100:], alpha=0.2,s=1,c='grey') plt.show()
import numpy as np import matplotlib.pyplot as plt
def SIR(n): S = [84980] I = [20] R = [15000] for i in range(n): S_old = S[-1] I_old = I[-1] R_old = R[-1] S_new = S_old - 0.0000294*S_old*I_old I_new = I_old + 0.0000294*S_old*I_old - 0.5*I_old R_new = R_old + 0.5*I_old S.append(S_new) I.append(I_new) R.append(R_new) return S,I,R
s, i, r = SIR(10)
s
i
r
plt.scatter(range(len(s)),s) plt.scatter(range(len(i)),i) plt.scatter(range(len(r)),r)
import numpy as np
L = [1,2,3,4,5]
arr = np.array(L)
arr**2
arr.cumsum()
np.sin(arr)
A matrix example
m = np.array([[0,1,2], [0,0,0], [6,5,3]])
m[0,1]
m[:,2]
canvas = np.array([[0 for j in range(20)] for i in range(20)])
canvas
plt.imshow(canvas)
canvas[5,10] = 1
canvas
plt.imshow(canvas)
image.shape
plt.imshow(image)