In :

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           #



# 1. Party game: squeezed

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 00 and 99.”
• Player: “42”.
• Chooser: “State a number between 00 and 42.”
• Player: “26”.
• Chooser: “State a number between 26 and 42.”

$\vdots$

• Chooser: “State a number between 29 and 32.”

• Player: “31”.
• Chooser dances some very silly children song.

Implement this game in Python, where the computer is the chooser.

Useful: $\mathtt{random.randint()}$ and $\mathtt{input()}$.

# 2. List coprehensions

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']


• 'numbers' is a list of lists. Using a list comprehension, flatten 'numbers' so it is a list of only numbers (not list of lists).
• use the newly flattened 'numbers' and filter it in a way that it only contains odd numbers.
• using a list comprehension, remove all words containing an 'i' from 'words'
• using a list comprehension, remove all words containing more than two vowels from 'words'.
• find all prime numbers between 1 and 100 using a single list comprehension

# 3. Cartesian/Polar Coordinates

Points may be given in polar $(r, \theta)$ or cartesian coordinates $(x, y)$, see Figure 1.

Figure 1. Relationship between polar and cartesian coordinates.

## 3.1 Polar to cartesian

Write a function $\mathtt{pol2cart}$, that takes a tuple $\mathtt{(r, θ)}$ in polar coordinates and returns a tuple in cartesian coordinates.

## 3.2 Cartesian to polar

Write the inverse function $\mathtt{cart2pol}$, such that $\mathtt{pol2cart( cart2pol( ( x,y) ) )}$ is $\mathtt{(x, y)}$ for any input $\mathtt{(x, y)}$.

## 3.3 Extend the two functions:

such that they can in addition handle lists of tuples.

# 4. A bit of statistics

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.

# 5. Some numpy foo

## 5.1 Defeat optical illusions

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 :

import scipy.ndimage
plt.imshow(im)
plt.grid(0)
plt.axis('Off')




Out:

(-0.5, 1023.5, 956.5, -0.5)



## 5.2 Compute $\pi$:

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 :

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:

<matplotlib.image.AxesImage at 0x7f21d8659080>



## 5.3 Twist and turn

Convert this image:

to



In :

plt.imshow(mos)




Out:

<matplotlib.image.AxesImage at 0x7f21d863db38>