{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Programming with Python\n",
"#### Stefan Güttel, [guettel.com](http://guettel.com)\n",
"\n",
"## Exercises: Modules"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 1.** Write a module with three functions:\n",
"* `has_prime_perm(n)` that returns `True` if any [permutation](http://en.wikipedia.org/wiki/Permutation) of `n`'s digits form a prime number, and\n",
"* `has_only_prime_perms(n)` that returns `True` if all of the permutations of `n`'s digits form prime numbers, and\n",
"* `prime_perms(n)` that returns either a list or a generator of all the prime numbers that can be formed by permuting the digits of `n`.\n",
"\n",
"Negative numbers have no prime permutations.\n",
"\n",
"Make sure that it is possible to run the module as an ordinary program, in which case it should load `n` and print the return values of all three functions.\n",
"\n",
"The module may contain other functions.\n",
"\n",
"**Hint:** Use [`itertools` module](https://docs.python.org/3/library/itertools.html) to find all the permutations of a number's digits."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 2.** Write a program to detect an average number of swaps of the selection sort of a list of length `n` with a given sample size with and without repeating elements.\n",
"\n",
"**Hint:** Computing the actual average number of swaps would require generating and sorting lists of all possible orderings, which is $n^n$ lists with not necessarily distinct elements and $n! = n (n-1) \\cdots 1$ lists with distinct elements. Even for a length as small as $n = 10$, we would have to generate and sort $10^{10}$ lists.\n",
"\n",
"Instead, load another number, let us call it `sample_size`, and the do the following:\n",
"1. Generate `sample_size` random lists with elements from $\\{0,1,\\dots,n-1\\}$. This will allow element repetitions, since you will do no checks to avoid that. Sort the lists, counting the swaps.\n",
"2. Start with some list, say `list(range(n))`, permute it randomly `sample_size` times (which will produce lists with only distinct elements), and then sort the obtained permutations, counting the swaps.\n",
"\n",
"Functions for both purposes can be found in [`random` module](https://docs.python.org/3/library/random.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 3.** Write a program that loads a text, line by line, until it loads an empty line. It then prints how many Python keywords were in it. You may assume that all the words are separated by one or more spaces.\n",
"\n",
"**Hint:** The only new issue here is how to recognize a Python **keyword**. Luckily, there is a standard Python module for that, and it can be found in [this list](https://docs.python.org/3/py-modindex.html).\n",
"\n",
"**Problem 3a.** Print how many times each of the keywords has appeared.\n",
"\n",
"**Hint:** Use a dictionary."
]
}
],
"metadata": {
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}