In [2]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'png'
import matplotlib
matplotlib.rcParams['figure.figsize'] = (15,12)
matplotlib.rcParams['xtick.labelsize'] = 35
matplotlib.rcParams['ytick.labelsize'] = 35
matplotlib.rcParams['font.size'] = 35
matplotlib.rcParams['axes.labelsize'] = 50
matplotlib.rcParams['axes.facecolor'] = 'white'
matplotlib.rcParams['axes.edgecolor'] = 'black'
matplotlib.rcParams['axes.linewidth'] = 5
matplotlib.rcParams['lines.linewidth'] = 10.0 # line width in points
matplotlib.rcParams['image.cmap'] = 'gray'
#matplotlib.rcParams['lines.color'] = 'blue' # has no affect on plot(); see axes.prop_cycle
matplotlib.rcParams['lines.markersize'] = 8 #
One guessing game, called “squeezed”, is very common in parties. It consists of a player, the chooser, who writes down a number between 00–99. The other players then take turns guessing numbers, with a catch: if one says the chosen number, he loses and has to do something daft. If the guessed number is not the chosen one, it splits the range. The chooser then states the part which contains the chosen number. If the new region only has one number, the chooser is said to be “squeezed” and is punished. An example of gameplay would be:
Chooser writes down (secretly) his number (let’s say, 30).
Chooser: “State a number between 26 and 42.”
$\vdots$
Chooser: “State a number between 29 and 32.”
Implement this game in Python, where the computer is the chooser.
Useful: $\mathtt{random.randint()}$ and $\mathtt{input()}$.
Given the following lists:
In [ ]:
numbers = [[1,2,3],[4,5,6],[7,8,9]]
words = ['if','i','could','just','go','outside','and','have','an','ice','cream']
Points may be given in polar $(r, \theta)$ or cartesian coordinates $(x, y)$, see Figure 1.
Figure 1. Relationship between polar and cartesian coordinates.
Write a function $\mathtt{pol2cart}$, that takes a tuple $\mathtt{(r, θ)}$ in polar coordinates and returns a tuple in cartesian coordinates.
Write the inverse function $\mathtt{cart2pol}$, such that $\mathtt{pol2cart( cart2pol( ( x,y) ) )}$ is $\mathtt{(x, y)}$ for any input $\mathtt{(x, y)}$.
such that they can in addition handle lists of tuples.
Draw $N=10000$ unifromly distributed random numbers (use np.random.uniform
, for example). Plot it's histogram and check that it looks uniform.
Now draw another such sample, and sum the two. How does the histogram of the sum look like?
Continue to sum $3,4,5,..$ such samples and keep plotting the histogram. It should quickly start to look like a gaussian.
This is a quite famous optical illusion:
The rows are perfectly straight, althought they appear crooked. Use numpy and slicing operations to verify for yourself that they are indeed so.
The code for loading the image as a numpy array is provided below:
In [2]:
import scipy.ndimage
im = scipy.ndimage.imread("images/ill.png")
plt.imshow(im)
plt.grid(0)
plt.axis('Off')
Out[2]:
Below is an array $Z$ which, when plotted, produces an image of a circle. Compute the value of $\pi$ by counting the number of black pixels in the array.
In [3]:
x = np.arange(-1,1, 0.01)
y = np.arange(-1,1, 0.01)
X,Y = np.meshgrid(x,y)
Z = X**2 + Y**2
Z = np.where(Z<1, 1, 0)
plt.matshow(Z)
Out[3]:
Convert this image:
to
In [4]:
mos = scipy.ndimage.imread("images/mosaic_grey.png")
plt.imshow(mos)
Out[4]: