CoCalc Public FilesThiel Workspace.ipynbOpen with one click!
Authors: Karim Ahmed, Julia Burnside, Sharalee Jones , Anika Khan, MIRALIA MOREAU, Tunazzina Mahdin, Mekeisha Naughton, David Ramirez, Katherine Rosero, Frank Russo, and 6 more authors...
Views : 396
Compute Environment: Ubuntu 20.04 (Default)

Fibonacci Sequence (dynamic programming)

In [1]:
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]
In [2]:
d_fib(10)
55
In [3]:
d_fib(100)
354224848179261915075
In [4]:
def fib(n): if n == 0 or n == 1: return n else: return fib(n-1) + fib(n-2)

The Lightbulb Problem

We compute the minimum number of packages needed to form nn bulbs if we have packages of size 1, 2, 6, and 8.

In [5]:
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]
In [7]:
bulb(30)
[0, 1, 1, 2, 2, 3, 1, 2, 1, 2, 2, 3, 2, 3, 2, 3, 2, 3, 3, 4, 3, 4, 3, 4, 3, 4, 4, 5, 4, 5, 4]
4

Maze path counter

The function pathspaths 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.

In [1]:
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]
In [2]:
maze = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
In [3]:
paths(maze)
[1, 1, 1, 1, 1] [1, 2, 3, 4, 5] [1, 3, 6, 10, 15] [1, 4, 10, 20, 35]
35

Largest Palindrome Substring

In [4]:
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
In [5]:
largeSub('aabbaaccaabac')
[1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0] [1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0] [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0] [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0] [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0] [1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0] [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0] [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
8

The Pizza Problem

In [2]:
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
In [3]:
pizza(0)
([2000], [2000])
In [4]:
pizza(1)
([2000, 1700.0], [2000, 2050.0])
In [6]:
mario, tony = pizza(15)
In [9]:
import matplotlib.pyplot as plt
In [13]:
plt.plot(mario,label="Mario's") plt.plot(tony,label="Tony's") plt.xlabel('Week') plt.ylabel('Pizzas Sold') plt.legend() plt.show()

The Bald Eagle Problem

In [16]:
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
In [22]:
juv, ad = eagle(25)
In [23]:
plt.plot(juv,label="juveniles") plt.plot(ad,label="adults") plt.xlabel('5 year period') plt.ylabel('population') plt.legend() plt.show()

The Logistic Map

In [24]:
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
In [34]:
plt.scatter(range(101),logistic(100,3.8)) plt.show()
In [ ]:
1.5, 2.3, 2.6, 3.1, 3.3, 3.5, 3.8

Logistic Bifurcation

In [35]:
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()

An SIR Model

In [1]:
import numpy as np import matplotlib.pyplot as plt
In [8]:
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
In [9]:
s, i, r = SIR(10)
In [10]:
s
[84980, 84930.03176, 84780.29448688294, 84332.33160744165, 82998.86767241065, 79088.80377684924, 68134.15016354085, 41471.74346402488, 848.683336366601, -580.5723043986347, -67.30977288559006]
In [11]:
i
[20, 59.96824000000001, 179.72139311706434, 537.8235759998248, 1602.3757230309218, 4711.251757076869, 13310.279491846819, 33317.54644543937, 57281.83335037797, 30070.172315954223, 14521.823626464065]
In [12]:
r
[15000, 15010.0, 15039.98412, 15129.84481655853, 15398.756604558443, 16199.944466073903, 18555.570344612337, 25210.710090535747, 41869.483313255434, 70510.39998844442, 85545.48614642154]
In [16]:
plt.scatter(range(len(s)),s) plt.scatter(range(len(i)),i) plt.scatter(range(len(r)),r)
<matplotlib.collections.PathCollection at 0x7f0f17606b20>

Intro to NumPy

In [5]:
import numpy as np
In [18]:
L = [1,2,3,4,5]
In [19]:
arr = np.array(L)
In [20]:
arr**2
array([ 1, 4, 9, 16, 25])
In [21]:
arr.cumsum()
array([ 1, 3, 6, 10, 15])
In [22]:
np.sin(arr)
array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427])

A matrix example

In [6]:
m = np.array([[0,1,2], [0,0,0], [6,5,3]])
In [24]:
m[0,1]
1
In [7]:
m[:,2]
array([2, 0, 3])
In [26]:
canvas = np.array([[0 for j in range(20)] for i in range(20)])
In [27]:
canvas
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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Image at 0x7f0f174b3b50>