0% found this document useful (0 votes)
240 views

Exercises CME

This document contains a series of exercises for learning Python. It covers basics like using the Python interpreter, writing scripts, variables and data types. It then discusses control flow with for and while loops. Functions, lists, tuples and dictionaries are also covered with exercises to practice creating and using these core Python concepts. The exercises range from simple tasks like printing output to more complex problems involving algorithms like finding prime numbers, longest word in a string, and Collatz sequences.

Uploaded by

TDLemonNh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views

Exercises CME

This document contains a series of exercises for learning Python. It covers basics like using the Python interpreter, writing scripts, variables and data types. It then discusses control flow with for and while loops. Functions, lists, tuples and dictionaries are also covered with exercises to practice creating and using these core Python concepts. The exercises range from simple tasks like printing output to more complex problems involving algorithms like finding prime numbers, longest word in a string, and Collatz sequences.

Uploaded by

TDLemonNh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CME 193

Introduction to Python

Exercises

Basics

Exercise 1.1: The interpreter


Open the Python interpeter. What happens when you input the following statements:
(a) 3 + 1
(b) 3 * 3
(c) 2 ** 3
(d) "Hello, world!"
Exercise 1.2: Scripts
Now copy the above to a script, and save it as script1.py. What happens if you run the script? (try:
python script1.py). Can you fix this (hint: use the print function)
Exercise 1.3: More interpreter
Explain the output of the following statements if executed subsequently:
(a) py + thon
(b) py * 3 + thon
(c) py - py
(d) 3 + 3
(e) 3 * 3
(f) a
(g) a = 3
(h) a
Exercise 1.4: Booleans
Explain the output of the following statements:
(a) 1 == 1
(b) 1 == True
(c) 0 == True
(d) 0 == False
(e) 3 == 1 * 3
(f) (3 == 1) * 3
(g) (3 == 3) * 4 + 3 == 1
(h) 3**5 >= 4**4
Exercise 1.5: Integers
Explain the output of the following statements:
(a) 5 / 3
(b) 5 % 3
(c) 5.0 / 3
(d) 5 / 3.0
(e) 5.2 % 3

CME 193

Introduction to Python

Exercises

(f) 2001 ** 200


Exercise 1.6: Floats
Explain the output of the following statements:
(a) 2000.3 ** 200 (compare with above)
(b) 1.0 + 1.0 - 1.0
(c) 1.0 + 1.0e20 - 1.0e20
Exercise 1.7: Variables
Write a script where the variable name holds a string with your name. Then, assuming for now your
name is John Doe, have the script output: Hello, John Doe! (and obviously, do not use print "Hello,
John Doe!".
Exercise 1.8: Type casting
Very often, one wants to cast variables of a certain type into another type. Suppose we have variable
x = 123, but really we would like x to be an integer.
This is easy to do in Python, just use desiredtype(x), e.g. int(x) to obtain an integer.
Try the following and explain the output
(a) float(123)
(b) float(123)
(c) float(123.23)
(d) int(123.23)
(e) int(123.23)
(f) int(float(123.23))
(g) str(12)
(h) str(12.2)
(i) bool(a)
(j) bool(0)
(k) bool(0.1)

Control flow

Disclaimer: Some of the following problems are inspired by problems from www.projecteuler.net. Have a
look if you are interested, there are some great challenges and Python is an excellent tool for solving them.
Exercise 2.1: Range
Type range(5) in the interpreter, what does the interpreter return? So what does for i in range(5)
mean?
Lets also find out whether the interpreter can help us understand the object range(5) better. Type
type(range(5)) in the interpreter. More on this soon!
Exercise 2.2: For loops
Use a for loop to:
(a) Print the numbers 0 to 100

Page 2

CME 193

Introduction to Python

Exercises

(b) Print the numbers 0 to 100 that are divisible by 7


(c) Print the numbers 1 to 100 that are divisible by 5 but not by 3
(d) Print for each of the numbers x = 2, . . . 20, all numbers that divide x, excluding 1 and x. Hence,
for 18, it should print 2 3 6 9.
Hint: see https://docs.python.org/2.7/library/functions.html#range.
Exercise 2.3: Simple while loops
Instead of using a for loop, use a while loop to:
(a) Print the numbers 0 to 100
(b) Print the numbers 0 to 100 that are divisible by 7
Exercise 2.4: While loops
Use a while loop to find the first 20 numbers that are divisible by 5, 7 and 11, and print them Hint:
store the number found so far in a variable.
Pseudo-code:
number found = 0
x = 11
while number found is less than 20:
if x is divisible by 5, 7 and 11:
print x
increase number found by 1
increase x by 1
Exercise 2.5: More while loops
The smallest number that is divisible by 2, 3 and 4 is 12. Find the smallest number that is divisible by
all integers between 1 and 10.
Exercise 2.6: Collatz sequence
A Collatz sequence is formed as follows: We start with some number x0 , and we find the next number
in the sequence by
(
xi /2
if xi is even
xi+1 =
3xi + 1 if xi is odd
If xi = 1, we stop iterating and have found the full sequence.
For example, if we start with x0 = 5, we obtain the sequence:
5 16 8 4 2 1
It is conjectured, though not proven, that every chain eventually ends at 1.
Print the Collatz sequence starting at x0 = 103.

Functions

Exercise 3.1: Hello


(a) Write a function hello_world that prints Hello, world!
(b) Write a function hello_name(name) that prints Hello, name! where name is a string.

Page 3

CME 193

Introduction to Python

Exercises

(c) Explain the difference between the print and return keywords. What would change if instead of
print you would use return?
Exercise 3.2: Polynomial
Write a function that evaluates the polynomial 3x2 x + 2.
Exercise 3.3: Maximum
Write a function my_max(x,y) that returns the maximum of x and y. Do not use the max function, but
use if instead in following two ways:
(a) Use both if and else.
(b) Use if but not else (nor elif).
Exercise 3.4: Primes
(a) Write a function is_prime(n) that returns True only if n is prime.
(b) Note that apart from 2 and 3, all primes are of the form 6k 1 (though not all numbers of the
form 6k 1 are prime of course). Using this, we can improve the computation time by a factor 3.
Update your function to use this.
(c) Write a function that prints all primes up to n.
(d) Write a function that prints the first n primes.
Exercise 3.5: Root finding
Suppose f is a continuous function and f (a) < 0 and f (b) > 0 for some known a and b. For simplicity,
assume a < b. Then, there must exist some c such that f (c) = 0.
(a) Write a function root(f, a, b) that takes a function f and two floats a and b and returns the
root c. Hint: check the sign at the midpoint of the interval.
(b) Remove the assumption that a < b, and that f (a) < 0 and f (b) > 0, if your current code relies on
them.
(c) Add a check that prints
function evals have same sign
if f (a) > 0 and f (b) > 0 or if f (a) < 0 and f (b) < 0.

Lists

Exercise 4.1: Short questions


(a) Write a function that prints the elements of a list
(b) Write a function that prints the elements of a list in reverse
(c) Write your own implementation of the len function that returns the number of elements in a list.
Exercise 4.2: Copying lists
(a) Create a list a with some entries.
(b) Now set b = a
(c) Change b[1]
(d) What happened to a?
(e) Now set c = a[:]
(f) Change c[2]

Page 4

CME 193

Introduction to Python

Exercises

(g) What happened to a?


Now create a function set_first_elem_to_zero(l) that takes a list, sets its first entry to zero, and
returns the list.
What happens to the original list?
Exercise 4.3: Lists and functions
Write a function that takes a list and an index, and sets the value of the list at the given index to 0.
Exercise 4.4: Primes
In Section 3 you wrote a function that prints all primes up to n, and a function that prints the first n
primes. Update these functions such that they return lists instead.
Exercise 4.5: List comprehensions
Let i, j = 1, . . . , n
(a) Generate a list with elements [i,j].
(b) Generate a list with elements [i,j] with i < j
(c) Generate a list with elements i + j with both i and j prime and i > j.
(d) Write a function that evaluates an arbitrary polynomial a1 xn + a2 xn1 + . . . an+1 using a list
comprehension, where you are given x and a list with coefficients coefs
Exercise 4.6: Flatten a list of lists
Consider having a list with lists as elements, e.g. [[1,3], [3,6]].
Write a function that takes such a list, and returns a list with as elements the elements of the sublists,
e.g. [1, 3, 3, 6].
Exercise 4.7: Finding the longest word
Write a function that returns the longest word in a variable text that contains a sentence. While text
may contain punctuation, these should not be taken into account. What happens with ties?
As an example, consider: Hello, how was the football match earlier today???
Exercise 4.8: Collatz sequence, part 2
Recall the Collatz sequence problem from Section 1. Our goal is to find the number n < 1, 000, 000 that
leads to the longest Collatz sequence.
(a) Write a function that for any n, returns its Collatz sequence as a list
(b) Write a function that finds the integer x that leads to the longest Collatz sequence with x < n.
Exercise 4.9: Sorting part 0
How would you sort a list of numbers?
Dont worry about any implementation, just think about methods that seem reasonable. We will implement a sorting algorithm later in the class.

Tuples

Exercise 5.1: Swapping two values


Suppose you have two variables: a and b. Now you want to set a equal to the value of b and at the
same time set b equal to the value of a.
The following obviously does not work

Page 5

CME 193

Introduction to Python

Exercises

a = b
b = a
so in some languages, you need to define a third variable like this
t = a
a = b
b = t
However, in Python you dont need to do this. How can you swap a and b in one line?
Exercise 5.2: Zip
Suppose we have two lists, x and y that give the x and y coordinates of a set of points. Create a list
with the coordinates (x,y) as a tuple. Hint: Find out about the zip function.
You have decided that actually, you need the two seperate lists, but unfortunately, you have thrown
them away. How can we use zip to unzip the list of tuples to get two lists again?
Exercise 5.3: Distances
Suppose we have two vectors, x and y, stored as tuples with n elements. Implement functions that
compute the l1 and l2 distances between x and y. Note that n is not explicitly given.

Dictionaries

Exercise 6.1: Printing a dictionary


Write a function that prints key-value pairs of a dictionary.
Exercise 6.2: Histogram
Write a function that takes a list, and returns a dictionary with keys the elements of the list and as
value the number of occurances of that element in the list.
After you are done, look up python collections counter in Google. Could you use a counter instead?
Exercise 6.3: Get method
Dictionaries have a get method, which takes a key and a default value. If the key is in the dictionary,
it returns the value, otherwise, it returns the default value.
Rewrite your code from the previous problem to make use of this get method.
Exercise 6.4: Vector functions
Lets implement some vector functions. There are two types of vectors, normal or dense vectors, which
we can represent using lists. For sparse vectors, where many of the elements are zero, this is inefficient.
Instead, we use a dictionary with keys the indices of non-zero values, and then the value corresponding
to the key is the value of the vector at that index. Hence, the vector [1, 2, 4] can be stored as a list: [1,
2, 4] or as a dictionary {0:1, 1: 2, 2: 4}.
(a) Write a function that adds two (dense) vectors
(b) Write a function that multiplies (i.e. inner product) two (dense) vectors
(c) Write a function that adds two sparse vectors
(d) Write a function that multiplies two sparse vectors
(e) Write a function that adds a sparse vector and a dense vector
(f) Write a function that multiplies a sparse vector and a dense vector
Exercise 6.5: Reverse look-up
Dictionaries are made to look up values by keys. Suppose however, we want to find the key that is

Page 6

CME 193

Introduction to Python

Exercises

associated with some value. Write a function that takes a dictionary and a value, and returns the key
associated with this value.
What challenges do you face? How would you deal with those challenges?

File I/O

Exercise 7.1: Open a file


Write a function that opens a file (input: filename), and prints the file line by line.
Exercise 7.2: Wordcount
On the course website you can find a text file containing the complete works of William Shapespeare.
Find the 20 most common words.
Also, find out how many unique words are used, and how many words are used more than five times.
Write the words that are used between 5 and 200 times to a file.
Exercise 7.3: Sum of lists
Before you start coding, please read the entire problem.
(a) Data generation
Write a function that takes three integers, n, a and b and a filename and writes to the file a list
with n random integers between a and b.
(b) Reading the data
Write a function that can read the files as generated above and return the values.
(c) Sum problem
Write a function that given two filenames (pointing to files as generated by the above function) and
an integer k, finds all u and v such that u + v = k, and u is an element of the first list and v is a
member of the second list.
(d) Testing
Test your functions by generating 2 files with n = 2000, a = 1, b = 10000 and k = 5000 and
k = 12000.
(e) Efficiency (optional)
If you are up to a challenge, write a function that solves the sum problem with the restriction that
you can only go over every number in the both lists once.

Numpy

Generate matrices A, with random Gaussian entries, B, a toeplitz matrix, where A Rnm and B Rmm ,
for n = 200, m = 500.
Exercise 8.1: Matrix operations
Calculate A + A, AA> , A> A and AB. Write a function that computes A(B I) for any .
Exercise 8.2: Solving a linear system
Generate a vector b with m entries and solve Bx = b.

Page 7

CME 193

Introduction to Python

Exercises

Exercise 8.3: Norms


Compute the Frobenius norm of A: kAkF and the infinity norm of B: kBk . Also find the largest and
smallest singular values of B.
Exercise 8.4: Power iteration
Generate a matrix Z, n n, with Gaussian entries, and use the power iteration to find the largest
eigenvalue and corresponding eigenvector of A. How many iterations are needed till convergence?
Optional: use the time.clock() method to compare computation time when varying n.
Exercise 8.5: Checkerboard
Create an 8x8 matrix with a checkerboard pattern. Find its SVD.
Exercise 8.6: Nearest neighbor
Write a function that takes a value z and an array A and finds the element in A that is closest to z. The
function should return the closest value, not index

Scipy

Exercise 9.1: Least squares


Generate matrix A Rmn with m > n. Also generate some vector b Rm .
Now find x = arg minx kAx bk2 .
Print the norm of the residual.
Exercise 9.2: Optimization
Find the maximum of the function

10

f (x) = sin2 (x 2)ex

f (x) = sin2 (x 2)ex

Matplotlib

Exercise 10.1: Plotting a function


Plot the function
over the interval [0, 2]. Add proper axis labels, a title, etc.

Exercise 10.2: Data


Create a data matrix X with 20 observations and 10 variables. Generate a vector b with parameters
Then generate the response vector y = Xb + z where z is a vector with standard normally distributed
variables.
Now (by only using y and X), find an estimator for b, by solving
b = arg min kXb yk2
b

Plot the true parameters b and estimated parameters b.


Exercise 10.3: Histogram and density estimation
Generate a vector z of 10000 observations from your favorite exotic distribution. Then make a plot that
shows a histogram of z (with 25 bins), along with an estimate for the density, using a Gaussian kernel
density estimator (see scipy.stats)

Page 8

CME 193

11

Introduction to Python

Exercises

Recursion

Exercise 11.1: Power


Write a recursive function that computes ab for given a and b, where b is an integer. Do not use **.
Exercise 11.2: Factorial
Write a recursive function to compute n! = n (n 1) . . . 1. Note that 0! is defined to equal 1.
Exercise 11.3: Recursive root finding
In Exercise 5 you wrote a function to find a root of a function f . Now write a recursive function that
finds the root of a function.
Exercise 11.4: Collatz sequence
Write a recursive implementation of a function that returns a list with the Collatz sequence started at
an arbitrary starting value.
Recall: A Collatz sequence is formed as follows: We start with some number x0 , and we find the next
number in the sequence by
(
xi /2
if xi is even
xi+1 =
3xi + 1 if xi is odd
If xi = 1, we stop iterating and have found the full sequence.
Exercise 11.5: Fibonacci sequence
The Fibonacci sequence {Fi }i = 0 starts with F0 = 0, F1 = 1. Every subsequent value in the sequence
is the sum of the last elements in the sequence:
Fn = Fn1 + Fn2
(a) Implement a non-recursive function that computes the Fn
(b) Implement a recursive function that computes Fn
(c) Compare the runtime for computing F35 recursively versus non-recursively, and explain the difference.
(d) This does not mean a recursion is not feasible for this problem, only that the naive implementation
isnt the best. We can get a better version using either of the following
(a) Store values already calculated.
(b) Generalizing the Fibonacci sequence to an additive sequence with arbitrary starting points t0
and t1 , and finding a recursive algorithm to find the nth term in such a sequence.
Implement one of the above (or both).
Exercise 11.6: Palindromes
Given a string t, we are interested in finding the largest palindrome in t, where we are allowed to
remove characters from t. For example, consider the string abcdba, then the function should return
abcba. Before you start coding, figure out the recursion on paper first.
Extra: you will notice that if you are not careful, this will take a very long time to compute for longer
inputs. However, a simple modification can speed up your code so that it runs in O(n2 ), where n is the
length of the input string. Find and implement this modification.1
Exercise 11.7: Quicksort
There are many sorting algorithms, see for example http://www.sorting-algorithms.com/. Quicksort
is a well known (and quick) sorting algorithm that works as follows:
1 This

exercise is inspired by an exercise in cs221.

Page 9

CME 193

Introduction to Python

Exercises

(a) Test whether the list is sorted, if not:


(b) Select one part of the list as pivot, any element works.
(c) Create a new list, left and right, and put all elements smaller than the pivot in the left list,
and all elements larger than the pivot in the right list.
(d) Recursively sort the left and right list, and return sorted left + pivot + sorted right.
Implement the quicksort algorithm. However, first think about the following caveats and how to get
around them:
(a) Testing whether a list is sorted takes quite a bit of work, so we do not want to do this. Instead, we
want to perform a much simpler check: what list is trivially sorted?
(b) What happens when the pivot element occurs multiple times in the list? How can we get around
this?

12

Classes

Exercise 12.1: Rational numbers


In this problem, we will write a class that can represent rational numbers, i.e. fractions pq .
(a) Create a class Rational which is initialized by two integers, p and q, the nominator and denominator
1
(b) We would like to represect 10
20 by 2 instead, hence write a function that computes the greatest
common divisor, and ensure that every rational number is simplified

(c) Add a method so that we can add two rational numbers with r1 + r2, here the __add__() method
is useful.
(d) Add a method to subtract two rational numbers. (__sub__)
(e) Add a method to multiply two rational numbers. (__mul__)
(f) Add a method to divide two rational numbers. (__div__)
(g) Add a method that compares whether two rational numbers are equal.
(h) Add a method to convert the rational number to a floating point (the __float__() method may
be handy).
(i) Add a method to print the rational number as p/q (the __str__() or __repr__ method is useful).
(j) Add any more functionality that you think is useful but I failed to mention.
Exercise 12.2: Binary search tree
In this exercise, we will implement a binary search tree. See http://en.wikipedia.org/wiki/Binary_
search_tree for an explanation.
(a) Define a class Node, and write the constructor, which takes one argument, value, and initializes
the left and right children to None.
(b) Write a function to print the tree.
(c) Write a function that inserts a new value in the tree at the right location.
(d) Write a function that looks up a value in the tree.
(e) Write a function that removes a value from the tree.

Page 10

CME 193

Introduction to Python

Exercises

Exercise 12.3: Ordinary least squares


Our goal in this exercise is to write our own least-squares solver to solve regression problems:
arg min ky Xk2

See for example statsmodels ols or LinearRegression. While one can, and should, use written solvers,
its a good practice exercise.
(a) Setup an OLS class with fit and predict methods, to be coded later
(b) Write the fit method using numpys or scipys linear algebra module.
(c) Now write the predict function, that predicts yn given new Xn .
(d) Add a function that summarizes the model
(e) (Optional) Use Patsy and Pandas to support DataFrames and formulas, similar to R.

13

Iterators

Exercise 13.1: Collatz sequence


Write a generator that generates the Collatz sequence with initial value n. Use this to print out the
Collatz sequence started at 103
Recall the Collatz sequence problem from last week. A Collatz sequence is formed as follows: We start
with some number x0 , and we find the next number in the sequence by
(
xi /2
if xi is even
xi+1 =
3xi + 1 if xi is odd
If xi = 1, we stop iterating and have found the full sequence.
Exercise 13.2: Collatz array using Numpy
Use the Collatz generator you wrote in the previous exercise to generate a vector with as ellements the
Collatz sequence started at 61.
Hint: use the np.fromiter function.
Exercise 13.3: Prime numbers
Write an iterator that iterates over the first n prime numbers. Use this to print out the first 10,000
primes.

14

Exception handling

Exercise 14.1: Rational numbers


Edit your code that implements the Rational class such that it raises an exception when the denominator
is 0.
Exercise 14.2: Wordcount
Recall the exercise of finding the 20 most common words in the Complete works of Shakespeare.
Edit your code such that you can specify the filename and the k most common words, and that the
script will print the k most common words, and their counts, from the file.
Make sure you handle errors gracefully, such as a misspecified filename.

Page 11

CME 193

15

Introduction to Python

Exercises

Unit testing

Exercise 15.1: Factorial


Write unit tests for the factorial function.
Exercise 15.2: Prime numbers
Write a program primes.py that takes two command line arguments, a < b, and returns all prime
numbers between a and b (inclusive). Write a seperate test script primestest.py that contains unittests
for all functions in your script.
Exercise 15.3: Sorting
Write unit tests for a sorting algorithm. Test whether your implementation of quicksort passes the
tests.

Page 12

You might also like