Format of the midterm exam

The midterm exam will be an open-note exam. In the exam I will ask you to write two or three short Python programs. You will be able to use the lecture notes for this exam, and you are welcome to copy any snippets of code that you might find useful from the course web site.

The exam will be a take-home exam. I will post the exam problems at 3:10 on Friday, Feb. 5. You will have until 3:10 on Saturday, Feb. 6 to send me your work.

The exam will cover Python if-else statements, loops, tuples, files, and functions.

Sample exam questions

1. The list x below contains a list of values xi. The list w below contains a list of weights wi.

x = [18.06, 15.57, 2.00, 1.27, 17.58, 17.19, 0.55, 6.03]
w = [-0.10, -0.20, -0.92, 0.31, 0.10, -0.54, 0.90, 0.88]

Write a program that computes and prints the weighted sum of the x values,

Solution

2. Two numbers are relatively prime if they have no factors in common. For example, the numbers 567 and 370 are relatively prime, while 567 and 378 are not relatively prime because they have 189 as a common factor. Write a program that prompts the user to enter two positive integers and then prints a message saying whether or not the numbers are relatively prime.

Solution

3. The list shown below contains several duplicate numbers. Write a program that sorts this list in ascending order and then prints the members of the list, but with each distinct number in the list printed only once.

A = [4, 15, 12, 5, 7, 15, 10, 5, 13, 8, 5, 7, 8, 1, 12, 7, 13, 12, 8]

Solution

4. Write a program that works with a text file named "numbers.txt" that contains a list of positive integers, one per line. The numbers in the list are all less than or equal to 20, and the list may contain repeated numbers. Have your program determine how many times each of the numbers from 1 to 20 appears in the file and then write this information to a second text file named "counts.txt". Each line in the output file should contain a number followed by the count of how many times it appeared in the input file.

Solution

5. Write the code for a function

merge(listOne,listTwo)

that takes as its parameters two list of integers. The numbers in the two lists are in no particular order, and numbers in the lists may be repeated. Your function should construct and return a new list that contains a list of the numbers that appear in the two lists, with none of the numbers repeated.

Write a test program that calls this function and then prints the resulting list to confirm that your function is working the way it should.

Solution