Commit 2ed3c571 authored by Michael Wimmer's avatar Michael Wimmer
Browse files

copy material over from the programming course

parent ab2cd451
# year hare lynx carrot
1900 30e3 4e3 48300
1901 47.2e3 6.1e3 48200
1902 70.2e3 9.8e3 41500
1903 77.4e3 35.2e3 38200
1904 36.3e3 59.4e3 40600
1905 20.6e3 41.7e3 39800
1906 18.1e3 19e3 38600
1907 21.4e3 13e3 42300
1908 22e3 8.3e3 44500
1909 25.4e3 9.1e3 42100
1910 27.1e3 7.4e3 46000
1911 40.3e3 8e3 46800
1912 57e3 12.3e3 43800
1913 76.6e3 19.5e3 40900
1914 52.3e3 45.7e3 39400
1915 19.5e3 51.1e3 39000
1916 11.2e3 29.7e3 36700
1917 7.6e3 15.8e3 41800
1918 14.6e3 9.7e3 43300
1919 16.2e3 10.1e3 41300
1920 24.7e3 8.6e3 47300
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# List of projects for day 1\n",
"\n",
"## 1. Basic exercises\n",
"\n",
"#### <font color='darkred'>Level: Absolute beginner<font color='black'>\n",
"\n",
"Here are some very basic exercises for the fundamentals of python\n",
"\n",
"### Play with the notebook\n",
"\n",
"Take the notebook used for the introduction top python and play with the examples.\n",
"\n",
"### Lists and loops\n",
"\n",
"Some basic exercises for python loops:\n",
"\n",
"- Write code for printing out a multiplcation table in the form\n",
" \n",
" ```Multiplication table for 3:\n",
" 1 x 3 = 3\n",
" 2 x 3 = 6\n",
" ...```\n",
" \n",
"- Fill a list with numbers from `0` to 10. Write another loop to add up all the numbers in the list \n",
"\n",
"- Write code to print a pattern of `*` as follows:\n",
" ```\n",
" *\n",
" * *\n",
" * * *\n",
" * * * *\n",
" * * *\n",
" * *\n",
" *\n",
" ```\n",
" Can you generalize your code such that you can print such a pattern for any number of rows?\n",
" \n",
"\n",
"\n",
"### If, if, if, ....\n",
"\n",
"In this exercise you will be making geometric pictures using `if`-clauses. To this end you will program a python function `f(x, y)` that takes inputs `x` and `y` ranging from 0 to 10, and that returns a number from 0 to 5. This function can then be plotted in a color plot.\n",
"\n",
"The color plot we have prepared for you in a helper function (you will learn in day 2 about how to plot things in python):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"range(5) + range(5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from itertools import chain\n",
"\n",
"for i in chain(range(5), reversed(range(4))):\n",
" print('*' * i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from helpers import plot_function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As an example, let's make a function that returns one value if `x < 5`, and another for `x >= 5`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def f(x, y):\n",
" if x < 5:\n",
" return 1\n",
" else:\n",
" return 4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now use the helper `plot_function` to represent our function `f(x, y)` as a color plot:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"plot_function(f)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write your own `f(x, y)` to reproduce pictures like the ones given below\n",
"\n",
"<img src='if_figure.png' size=1000px>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Programming for biologists\n",
"\n",
"#### <font color='darkred'>Level: Beginner<font color='black'>\n",
"\n",
"Here are two basic problems from a python programming course expecially [aimed at biologists](http://www.programmingforbiologists.org)\n",
"\n",
"### Body mass of dinosaurs\n",
"\n",
"The length of an organism is typically strongly correlated with it's body mass. This is useful because it allows us to estimate the mass of an organism even if we only know its length. This relationship generally takes the form:\n",
"\n",
"$$ \\text{Mass} = a * \\text{Length}^b$$\n",
"\n",
"Where the parameters $a$ and $b$ vary among groups, mass is given in kg, and length in m. This allometric approach is regularly used to estimate the mass of dinosaurs since we cannot weigh something that is only preserved as bones.\n",
"\n",
"Different values of $a$ and $b$ are for example ([Seebacher 2001](http://www.jstor.org/stable/4524171\n",
"))\n",
"\n",
"[Therapoda](https://en.wikipedia.org/wiki/Theropoda) (e.g. T-rex): $a=0.73$, $b=3.63$\n",
"[Sauropoda](https://en.wikipedia.org/wiki/Sauropoda) (e.g. Brachiosaurus): $a = 214.44$, $b = 1.46$\n",
"\n",
"get_mass_from_length() that estimates the mass of an organism in kg based on it's length in meters by taking length, a, and b as parameters. To be clear we want to pass the function all 3 values that it needs to estimate a mass as parameters. This makes it much easier to reuse for all species.\n",
"\n",
"Use this function to compute the mass of [T-rex Trix](https://en.wikipedia.org/wiki/Trix_(dinosaur)) on display in Naturalis, Leiden, which is 13 meters long. \n",
"Compare to the [Camarasaurus](https://www.naturalis.nl/nl/kennis/collectie/topstukken/camarasaurusskelet/) they have there, too.\n",
"\n",
"### DNA vs RNA\n",
"\n",
"Write a function, `dna_or_rna(sequence`), that determines if a sequence of base pairs is DNA, RNA, or if it is not possible to tell given the sequence provided. Since all the function will know about the material is the sequence the only way to tell the difference between DNA and RNA is that RNA has the base Uracil (u) instead of the base Thymine (t). Have the function return one of three outputs: 'DNA', 'RNA', or 'UNKNOWN'. Use the function and a for loop to print the type of the sequences in the following list.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sequences = ['ttgaatgccttacaactgatcattacacaggcggcatgaagcaaaaatatactgtgaaccaatgcaggcg',\n",
" 'gauuauuccccacaaagggagugggauuaggagcugcaucauuuacaagagcagaauguuucaaaugcau',\n",
" 'gaaagcaagaaaaggcaggcgaggaagggaagaagggggggaaacc',\n",
" 'guuuccuacaguauuugaugagaaugagaguuuacuccuggaagauaauauuagaauguuuacaacugcaccugaucagguggauaaggaagaugaagacu',\n",
" 'gauaaggaagaugaagacuuucaggaaucuaauaaaaugcacuccaugaauggauucauguaugggaaucagccggguc']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Optional: For a little extra challenge make your function work with both upper and lower case letters, or even strings with mixed capitalization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Project Euler\n",
"\n",
"#### <font color='darkred'>Level: Beginner - as complicated as you want<font color='black'>\n",
"\n",
"Go to https://projecteuler.net/. It's a website offering a collection of mathy programming challenges, starting from very simple ones and ending with very hard ones. The first 10-20 problems are a great playground for playing with a new programming language. The problems above 50-100 require advanced knowledge of math and algorithms.\n",
"\n",
"Let's have a look at the [first problem](https://projecteuler.net/problem=1):\n",
"\n",
" Problem 1\n",
" \n",
" If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.\n",
"\n",
" Find the sum of all the multiples of 3 or 5 below 1000.\n",
"\n",
"How can you implement this on a computer? You have to loop over all the numbers and check if they are\n",
"divisible by 3 or 5. If yes, you add the number. \n",
"\n",
"If you would like to have some more hints, click <a href=\"#hinteuler1\" data-toggle=\"collapse\">here</a>.\n",
"\n",
"<div id=\"hinteuler1\" class=\"collapse\">\n",
"To loop over all the numbers below 1000, you can use a `for i in range(1000)` loop. To check if\n",
"a number is divisible by 3 or 5, use an `if` expression, and you can use `i % 3 == 0` to check if a number\n",
"is divisible by 3, for example. Finally, you should define a variable `total = 0` before the loop, and\n",
"add numbers to this variable inside the loop!\n",
"</div>\n",
"\n",
"In similar ways, you can tackle the other Euler problems!\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Cryptography \n",
"\n",
"### Caesar's cipher - trial and error\n",
"\n",
"#### <font color='darkred'>Level: Beginner<font color='black'>\n",
"\n",
"The following text is encrypted by shifting all letters in the alphabet by a fixed amount\n",
"(e.g. a shift by 2 would give A -> C, B -> D, ..., Z -> B). Decrypt it by trying out all possible shifts! (Just copy the assignment below to use the text in your python program)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"encrypted_text1 = \"\"\"\n",
"RW. XMJWQTHP MTQRJX, BMT BFX ZXZFQQD AJWD QFYJ NS YMJ RTWSNSLX, XFAJ ZUTS YMTXJ\n",
"STY NSKWJVZJSY THHFXNTSX BMJS MJ BFX ZU FQQ SNLMY, BFX XJFYJI FY YMJ GWJFPKFXY YFGQJ.\n",
"N XYTTI ZUTS YMJ MJFWYM-WZL FSI UNHPJI ZU YMJ XYNHP BMNHM TZW ANXNYTW MFI QJKY GJMNSI\n",
"MNR YMJ SNLMY GJKTWJ. NY BFX F KNSJ, YMNHP UNJHJ TK BTTI, GZQGTZX-MJFIJI, TK YMJ XTWY\n",
"BMNHM NX PSTBS FX F \"UJSFSL QFBDJW.\" OZXY ZSIJW YMJ MJFI BFX F GWTFI XNQAJW GFSI SJFWQD\n",
"FS NSHM FHWTXX. \"YT OFRJX RTWYNRJW, R.W.H.X., KWTR MNX KWNJSIX TK YMJ H.H.M.,\" BFX\n",
"JSLWFAJI ZUTS NY, BNYM YMJ IFYJ \"1884.\" NY BFX OZXY XZHM F XYNHP FX YMJ TQI-KFXMNTSJI\n",
"KFRNQD UWFHYNYNTSJW ZXJI YT HFWWD—INLSNKNJI, XTQNI, FSI WJFXXZWNSL. \n",
"\n",
"\"BJQQ, BFYXTS, BMFY IT DTZ RFPJ TK NY?\" \n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To this end, write a *function* that takes an encrypted text as input as well as a shift, and that then\n",
"prints the decrypted string.\n",
"\n",
"If you would like to have some hints, click <a href=\"#hintcrypto1\" data-toggle=\"collapse\">here</a>.\n",
"\n",
"<div id=\"hintcrypto1\" class=\"collapse\">\n",
"Remember, you can loop over all letters in a string using `for c in encrypted_text1:`. Also, you can\n",
"check if a letter is between \"A\" to \"Z\" by `\"A\" <= c and c <= \"Z\"`. One way of doing the subsitution is\n",
"to generate a dictionary with letters as keys and as values, and then use this to do the substitution in\n",
"the `for`-loop.\n",
"</div>\n",
"\n",
"A typical mistake you can find <a href=\"#hintcrypto2\" data-toggle=\"collapse\">here</a>.\n",
"\n",
"<div id=\"hintcrypto2\" class=\"collapse\">\n",
"There's a subtle logical mistake that is often done here: you might be compelled to first replace all `A`'s \n",
"in the string with another letter. Say the shift is 1, and that letter is `B`. Then you will have in your string\n",
"two types of `B`'s: decrypted (from replacing `A`) and still encrypted (the `B` from the original encrypted string). If you now replace both type of `B`'s in the next step, you run into problems ...\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Caesar's cipher - frequency analysis\n",
"\n",
"#### <font color='darkred'>Level: Intermediate<font color='black'>\n",
"\n",
"The Caesar's cypher can be broken by *frequency analysis*: Letters occur in a text with a certain probability. For example, a probability distribution for the English langiage can be found at [wikipedia](https://en.wikipedia.org/wiki/Frequency_analysis).\n",
"\n",
"Make a probability analysis of the text above. Plot the histogram of the letters using `print` statements, that looks like \n",
"\n",
" A ****\n",
" B ********\n",
" C **\n",
" \n",
"etc. From that, determine the shift of the Caeser's cypher and decrypt it in one go.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### A step aside: Frequency analysis of English texts\n",
"\n",
"#### <font color='darkred'>Level: Intermediate<font color='black'>\n",
"\n",
"In the previous exercise, you were asked to break a cypher using the frequency of letters as given in [wikipedia](https://en.wikipedia.org/wiki/Frequency_analysis). \n",
"\n",
"You can create such a histogram of frequency of letters yourself. For this, download the text of \"Romeo and Juliet\" from the file `romeo_and_juliet.txt` (that was downloade from [project Gutenberg](https://www.gutenberg.org/ebooks/1112.txt.utf-8)), and count the occurences\n",
"of all letters A-Z (treating upper case A-Z and lower case a-z the same). Do you find the same result as in the wikipedia article?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Substitution cipher\n",
"\n",
"#### <font color='darkred'>Level: Intermediate - Advanced<font color='black'>\n",
"\n",
"Now let's look at a more complicated cipher where we replace (uniquely) every letter by some other letter. This is not just described by a simple shift as before, but now we need to find a different mapping for every letter!\n",
"\n",
"It is a bit difficult to do this by frequency analysis of single letters (although you might identify certain letters). It is easier to do this by doing a frequency analysis of [bigrams](https://en.wikipedia.org/wiki/Bigram#Bigram_frequency_in_the_English_language), i.e. two letter combinations.\n",
"\n",
"Write a code to find out the probability of single letters and of the 10 most frequent bigrams. Use this as input to decypher the following text:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"encrypted_text2 = \"\"\"\n",
" \"IEGG ZE, DMISTW, DBMI FT RTN ZMOE TQ TNY LPSPITY'S SIPHO? SPWHE DE BMLE XEEW ST\n",
"NWQTYINWMIE MS IT ZPSS BPZ MWF BMLE WT WTIPTW TQ BPS EYYMWF, IBPS MHHPFEWIMG STNLEWPY\n",
"XEHTZES TQ PZJTYIMWHE. GEI ZE BEMY RTN YEHTWSIYNHI IBE ZMW XR MW EUMZPWMIPTW TQ PI.\"\n",
" \"P IBPWO,\" SMPF P, QTGGTDPWV MS QMY MS P HTNGF IBE ZEIBTFS TQ ZR HTZJMWPTW, \"IBMI FY.\n",
"ZTYIPZEY PS M SNHHESSQNG, EGFEYGR ZEFPHMG ZMW, DEGG-ESIEEZEF SPWHE IBTSE DBT OWTD BPZ\n",
"VPLE BPZ IBPS ZMYO TQ IBEPY MJJYEHPMIPTW.\"\n",
" \"VTTF!\" SMPF BTGZES. \"EUHEGGEWI!\"\n",
" \"P IBPWO MGST IBMI IBE JYTXMXPGPIR PS PW QMLTNY TQ BPS XEPWV M HTNWIYR JYMHIPIPTWEY\n",
"DBT FTES M VYEMI FEMG TQ BPS LPSPIPWV TW QTTI.\"\n",
" \"DBR ST?\"\n",
" \"XEHMNSE IBPS SIPHO, IBTNVB TYPVPWMGGR M LEYR BMWFSTZE TWE BMS XEEW ST OWTHOEF MXTNI\n",
"IBMI P HMW BMYFGR PZMVPWE M ITDW JYMHIPIPTWEY HMYYRPWV PI. IBE IBPHO-PYTW QEYYNGE PS\n",
"DTYW FTDW, ST PI PS ELPFEWI IBMI BE BMS FTWE M VYEMI MZTNWI TQ DMGOPWV DPIB PI.\"\n",
" \"JEYQEHIGR STNWF!\" SMPF BTGZES.\n",
" \"MWF IBEW MVMPW, IBEYE PS IBE 'QYPEWFS TQ IBE H.H.B.' P SBTNGF VNESS IBMI IT XE IBE\n",
"STZEIBPWV BNWI, IBE GTHMG BNWI IT DBTSE ZEZXEYS BE BMS JTSSPXGR VPLEW STZE SNYVPHMG\n",
"MSSPSIMWHE, MWF DBPHB BMS ZMFE BPZ M SZMGG JYESEWIMIPTW PW YEINYW.\"\n",
" \"YEMGGR, DMISTW, RTN EUHEG RTNYSEGQ,\" SMPF BTGZES, JNSBPWV XMHO BPS HBMPY MWF GPVBIPWV\n",
"M HPVMYEIIE. \"PWIEYESIPWV, IBTNVB EGEZEWIMYR,\" SMPF BE MS BE YEINYWEF IT BPS QMLTNYPIE\n",
"HTYWEY TQ IBE SEIIEE. \"IBEYE MYE HEYIMPWGR TWE TY IDT PWFPHMIPTWS NJTW IBE SIPHO.\n",
"PI VPLES NS IBE XMSPS QTY SELEYMG FEFNHIPTWS.\"\n",
" \"BMS MWRIBPWV ESHMJEF ZE?\" P MSOEF DPIB STZE SEGQ-PZJTYIMWHE. \"P IYNSI IBMI IBEYE PS\n",
"WTIBPWV TQ HTWSEANEWHE DBPHB P BMLE TLEYGTTOEF?\"\n",
" \"P MZ MQYMPF, ZR FEMY DMISTW, IBMI ZTSI TQ RTNY HTWHGNSPTWS DEYE EYYTWETNS.\"\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hint: Don't try to do everything in one go. As you have identified certain letters, substitute those in the text, and then guess other letters. Decrypt the text thus step by step."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. Babynames\n",
"\n",
"#### <font color='darkred'>Level: Intermediate<font color='black'>\n",
"\n",
"The file `babynames.txt` contains data on all names given to male children in the Netherlands in 2015 (from [SVB](https://www.svb.nl/int/nl/kindernamen/artikelen/top20/jongens/index.jsp). I apologize for only giving boy's names - for some reason the SVB didn't give a file with all girl's names)\n",
"\n",
"Read the file and do some analysis of the data:\n",
"\n",
"- Find the most common name. How many percent of Dutch male children were named like this in all of 2015?\n",
"- Find the shortest/longest name\n",
"- Find the name with the most special characters (\"special\" = not A-Z)\n",
"- Find the top 20 names, and give the percentage how often children were named like this. What is the total percentage of the top 20 names, i.e. how often are top 20 names given?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6. Find the zero of a function\n",
"\n",
"#### <font color='darkred'>Level: Intermediate to Advanced<font color='black'>\n",
"\n",
"Write a function to find the zero of a mathematical function $4f(x)$ within an interval $[a, b]$, when $f(a)$ and $f(b)$ have opposite sign (this makes sure there is at least one zero in this interval.\n",
"\n",
"Find this zero by *bisecting* the interval: Take the middle point $c$, and find out which interval now must have the zero by comparing the signs of the $f(a)$, $f(b)$, and $f(c)$. Iterate this procedure to zoom in on\n",
"the zero.\n",
"\n",
"Write a python function `find_root(f, a, b)` that implements this procedure. This python function should take another python function `f` as input, as well as two numbers `a` and `b`.\n",
"\n",
"*Possible extensions, optional*: \n",
"- Have another input parameter specifying the numerical accuracy with which you want to find the zero.\n",
"- Check the user input to make sure `find_root` can actually find a root."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
This diff is collapsed.
This diff is collapsed.
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
font = {'family' : 'sans',
'size' : 14}
matplotlib.rc('font', **font)
def plot_function(f):
n = 100
C = np.zeros(shape=(n, n))
for i in range(C.shape[0]):
for j in range(C.shape[1]):
C[i, j] = f(j/n*10, i/n*10)
fig = plt.figure(figsize=(5.4, 4))
ax = fig.add_subplot(111, aspect='equal')
plt.pcolormesh(np.linspace(0, 10, n), np.linspace(0, 10, n),
C, cmap='jet', vmin=0, vmax=5)
plt.colorbar()
plt.xlabel('x')
plt.ylabel('y')
plt.show()
This diff is collapsed.
This diff is collapsed.
qubit q0 # Define a qubit
h q0 # Apply a Hadamard gate
measure q0 # Measure in computational basis
#
# File: test1.qasm
# Date: 22-Mar-04
# Author: I. Chuang <ichuang@mit.edu>
#
# Sample qasm input file - EPR creation
#
qubit q0
qubit q1
h q0 # create EPR pair
cnot q0,q1
#
# File: test3.qasm
# Date: 22-Mar-04
# Author: I. Chuang <ichuang@mit.edu>
#
# Sample qasm input file - simple teleportation circuit
#
qubit q0
qubit q1
qubit q2
h q1 # create EPR pair
cnot q1,q2
cnot q0,q1 # Bell basis measurement
h q0
nop q1
measure q0
measure q1
c-z q1,q2 # correction step
c-x q0,q2
#
# File: test4.qasm
# Date: 22-Mar-04
# Author: I. Chuang <ichuang@mit.edu>
#
# Sample qasm input file - swap circuit
#
qubit q0
qubit q1
cnot q0,q1
cnot q1,q0
cnot q0,q1
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment