Previous topic

Extra practice with while loops

Next topic

Assignment #1: Million Song Dataset

CS 2120: Practice Final Exam¶

Instructions¶

You may use any resource you want. Textbooks, the internet, each other, a friend, a librarian, whatever. You can even test your answers to coding questions right in your own Python interpreter.

Please do cite your sources, though. There will be no marks deducted for using external resources (in fact, it is heavily encouraged), just give credit where it’s due.

1. Create a plain text file (using TextWrangler, SciTE, Notepad, etc) with the name your_last_name.txt. For example, I’d create daley.txt.

2. DO NOT use Microsoft Word or some other word processor for this. The file should be a plain text file.

3. Seriously, if you use a word processor, you get a zero on the final and be required to play the bassoon

4. Put your answers to each question in the text file, like this:

1.

2.

etc
5. When you’re done, submit your text file to Edmodo under the ‘Final Exam’ assignment.

Let’s get started.

The actual exam questions¶

1. [3 marks] Write a Python function exams_over_soon(n) to print Soon, exams will be over to the screen n times.

2. [2 marks] Explain (briefly!) the difference between a natural language and a formal language (like a programming language).

3. [2 marks] Fix the errors in the following Python code, which is meant to return the sum of a and b:

fed add_two_numbers(a,b,):
c = a -b+
return a
4. [2 marks] Fix the errors in the following Python code, which is meant to return the result of dividing a by b:

def divide(a,b):
return a/b

5. [3 marks] What types would Python automatically assign to the variable a in the following statements:
1. a = 5
2. a = 5.3
3. a = "i wonder whats on reddit right now..."
6. [3 marks] Write a function compmag(r,m) to compute, and return, the magnitude of a complex number. It should take the real component of the number as parameter r and the imaginary component as m. Remember that

7. [3 marks] Write a function hail(n) that will return the value n/2 if n is even and 3*n+1 if n is odd.

8. [2 marks] What is a CSV file? Are there any limitations on what a CSV file can store?

9. [3 marks] Explain, in English, the difference between an integer and a floating point number. In what ways does the behaviour of a floating point number deviate from our expectations (i.e., what’s weird about them)?

10. [5 marks] Write a function collatz(n) which does the following: while the value of n is not 1, set the value of n to the result of calling your hail(n) function from question 7. Print out the value of n after each call to hail.

11. [2 marks] What is the result of running collatz(8)? What is the result of running collatz(7)?

11b. [BONUS] What is the behaviour of this strange function collatz? Do you think it will always reach 1, for
any positive integer input? Can you argue your case?
1. [10 marks] Write a function solve_cos() that uses Newton’s method to solve the equation and returns a solution x.

2. [2 marks] Write a single-line Python command to concatenate the first 3 letters of the string a, plus the last 5 letters of the string b. Assume that variables containing strings named a and b already exist.

3. [3 marks] Write a function has_an_x(s) that returns True if the string s has an x or X in it and False otherwise.

4. [2 marks] Explain the differences between a Python list and a NumPy array.

5. [3 marks] Write a function sum_list(l) that returns the sum of all the elements of the list l. Assume that l contains only integers.

6. [3 marks] Write a function sum_array(l) that returns the sum of all the elements of the NumPy array l. Assume that l contains only integers.

7. [2 marks] Write a single-line Python command to produce a list of integers from 7 to 31.

8. [3 marks] Suppose I type the following commands:
>>> l = [1,2,3,4,5]
>>> a = l
>>> b = l[:]


What is the difference between a and b?

9. [5 marks] Write a function insert_after_beer(l,newitem) that will return a list with the following property: if the item  ‘beer’  appears in the list l, the function will insert the item newitem immediately after beer. Otherwise, it will leave l alone.

10. [3 marks] Are these functions pure? If not, what is/are their side effect(s)?:

def f(l):
return l[3:5]

def g(l):
l = l[3:5]
return

def h(l):
l = l[3:5]
return l

11. [2 marks] Write a single-line Python command to extract the ‘middle’ of a 2D NumPy array. By the ‘middle’, I mean “everything in the array except for the first and last columns and the first and last rows”.

12. [5 marks] Write a function airports() that:
1. Loads a CSV file directly from the internet located here: https://dl.dropbox.com/u/8011180/2120/data/airports.csv
2. Creates a dictionary from the CSV file such that each airport’s code is the key and the name is the value
3. Returns the dictionary
13. [4 marks] Execute the following Python code:
>>> import numpy
>>> d1 = numpy.random.normal(0,10,size=1000)
>>> d2 = numpy.random.normal(5,10,size=1000)
>>> d3 = numpy.random.poisson(size=1000)
>>> d4 = d2 + 1.0 + numpy.random.normal(1,2,size=1000)


What can you tell me about d1,d2,d3 and d4? Do some plots to back up your answer.

14. [5 marks] Explain, in English, how binary search works. If walking through an example is helpful, do so.

15. [10 marks] Consider the following function:

def my_function(inlist):

done = False

while not done:
done = True

i=0
while i < len(inlist):
print i
if inlist[i] > inlist[i+1]:
tmp = inlist[i]
inlist[i]=inlist[i+1]
inlist[i+1]=tmp
done = False
i += 1

return inlist


Now:

1. What do you think this function is meant to do? If this is an algorithm you recognize, name it.
2. Fix the error in this function.
3. How many times will we go around the two while loops for an input list with n elements?
16. [10 marks] Consider the following function:

def func(inlist):
lesser=[]
greater=[]
if len(inlist) <= 1:
return inlist
for i in inlist[1:]:
if i < inlist[0]:
lesser.append(i)
else:
greater.append(i)
return func(lesser)+inlist[0:1]+func(greater)


Now:

1. What do you think this function is meant to do? If this is an algorithm you recognize, name it.
2. What special technique does this function use?
3. How many times will the function call itself for an input list with n elements?
17. There is no question 28.

18. [5 marks] Below is some pseudocode describing a function which uses a Monte Carlo Method to compute pi:

num_itr = 30000

tries = 0
hits = 0
for i from 0 to num_itr
tries = tries + 1
x = random number between 0 and 1
y = random number between 0 and 1

dist = square root of x^2 + y^2

if dist <= 1.0
hits = hits + 1

pi = 4 * (hits/throws)
Do the following:
1. Convert the pseudocode to a working Python Function (be really careful about the hits/throws... the answer should be a floating point number).
2. Experiment with different values for num_itr. How does this change the accuracy of the result?
19. [4 marks] Explain the difference between supervised, and unsupervised, machine learning. Give one example of where you’d use each?

20. [2 marks] What are the two things that every recursive function requires?

21. [3 marks] Do question 1 again, but without using loops.

22. [5 marks] Write a function crosslist(l1,l2) that will take two lists (of strings) and return a single list that contains every combination of strings with prefixes from l1 and suffixes from l2. For example:

>>> l1 = ['a','b']
>>> l2 = ['c','d','e']
>>> crosslist(l1,l2)