CoCalc Public FilesFinals / Final Exam Review Solutions S21.ipynb
Author: Johann Thiel
Views : 89
Compute Environment: Ubuntu 20.04 (Default)

# Final Exam Review

In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


## 3 Dice

In [2]:
def roll():
sumOfRoll = np.random.randint(1,7,size=3).sum()
if sumOfRoll%4 == 0:
return 1
else:
return 0

s = 0
for i in range(10000):
s += roll()
s/10000

0.2519

## 3 Diamonds

In [3]:
values = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
suits = ['H','D','S','C']

cards = [(v,s) for v in values for s in suits]

def cardGame():
np.random.shuffle(cards)
hand = cards[:6]
diamondCount = 0
faceCount = 0
for card in hand:
if card[1] == 'D':
diamondCount += 1
if card[0] in ['J','Q','K']:
faceCount += 1
if diamondCount == 3 and faceCount == 1:
return 1
else:
return 0

s = 0
for i in range(10000):
s += cardGame()
s/10000

0.0523

## Area under $\ln^2(x+1)$

In [4]:
from math import log

def areaLogEst(N):
x = 4*np.random.random(size=N)
y = 4*np.random.random(size=N)
s = 0
for i in range(len(x)):
if log(x[i]+1)**2 >= y[i]:
s += 1
return s/N*16

areaLogEst(10000)

4.9168

## Area under curve $x^{1/2}+y^{1/2}=1$ in Q1

In [5]:
def areaCurve(N):
x = np.random.random(size=N)
y = np.random.random(size=N)
s = 0
for i in range(len(x)):
if x[i]**0.5 + y[i]**0.5 <= 1:
s += 1
return s/N

areaCurve(10000)

0.163

## Recurrence relation

In [6]:
def a(n):
L = [1,1,2] + [0 for i in range(n-3)]
for i in range(3,n):
L[i] = 3*L[i-1] - L[i-2] - 2*L[i-3]
return L[n-1]

a(100)

354224848179261915075

## Knapsack Problem

In [7]:
def knapsack(V,W,weight):
L = [[0 for j in range(weight+1)] for i in range(len(V))]
for j in range(2,weight+1):
L[0][j] = V[0]
for i in range(1,len(V)):
for j in range(1,weight+1):
if j < W[i]:
L[i][j] = L[i-1][j]
else:
L[i][j] = max(V[i]+L[i-1][j-W[i]],L[i-1][j])
for line in L:
print(line)
return L[len(V)-1][weight]

knapsack([20,30,25,32,15,20],[2,5,4,6,1,7],15)

[0, 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20] [0, 0, 20, 20, 20, 30, 30, 50, 50, 50, 50, 50, 50, 50, 50, 50] [0, 0, 20, 20, 25, 30, 45, 50, 50, 55, 55, 75, 75, 75, 75, 75] [0, 0, 20, 20, 25, 30, 45, 50, 52, 55, 57, 75, 77, 82, 82, 87] [0, 15, 20, 35, 35, 40, 45, 60, 65, 67, 70, 75, 90, 92, 97, 97] [0, 15, 20, 35, 35, 40, 45, 60, 65, 67, 70, 75, 90, 92, 97, 97]
97

## SIR Model

In [8]:
def sir(n):
S = [12000-20]
I = [20]
R = [0]
b = 36/((12000-20)*20)
for i in range(n):
S_old = S[-1]
I_old = I[-1]
R_old = R[-1]
S_new = S_old - b*S_old*I_old
I_new = I_old + b*S_old*I_old - I_old/2
R_new = R_old + I_old/2
S.append(S_new)
I.append(I_new)
R.append(R_new)
return S,I,R

s, i, r = sir(30)
plt.plot(s,label='susceptibles')
plt.plot(i,label='infected')
plt.plot(r,label='removed')
plt.legend()

max(i)

5665.357423222809

## Image flip

In [9]:
m = plt.imread('/home/user/DataSets/owl.png')

plt.imshow(m[::-1])

<matplotlib.image.AxesImage at 0x7f0e35d6b850>

## Image crop

In [10]:
plt.imshow(m[:,:160,:])

<matplotlib.image.AxesImage at 0x7f0e35ce1430>

## Low population countries

In [11]:
df = pd.read_csv('/home/user/DataSets/country_pop.csv')

In [12]:
df[df.Pop2019<=10000]

Country Pop2019 Pop2018 Area
63 Falkland Islands 3377.0 3234.0 12173.00
135 Montserrat 4989.0 4993.0 102.00
148 Niue 1615.0 1620.0 260.00
170 Saint Barthélemy 9847.0 9816.0 21.00
174 Saint Pierre and Miquelon 5822.0 5849.0 242.00
207 Tokelau 1340.0 1319.0 12.00
224 Vatican City 799.0 801.0 0.44