Problem 1. In each case, explain the Python output for... sions. A. 20+20/4 B. 20/4∗2

advertisement

40 pts.

40 pts.

40 pts.

40 pts.

40 pts.

Problem 1 . In each case, explain the Python output for each of these expressions.

A. 20+20/4

B. 20/4 ∗ 2

C. 2 ∗∗ 6/4

Problem 2 . Write a function bestof4 that accepts four integer grades and returns the average of the best 3 grades, rounded to the nearest integer. Use builtin Python functions.

Problem 3 . Suppose we have a function defined as follows def f ( a , b ) : x = a + b a= b ∗ x return y+a

Trace what happens in our memory model as each line of the following code is executed.

x = 2 y = 3 y = f ( x , y )

Problem 4 . Trace the following code line by line in our memory model and give the output.

A = [ 6 , 3 , 4 ]

B = A

C = A [ 3 ]

B [ 3 ] = 17 print A print B print C

Problem 5 . Write a function sublistsums(L) whose input is a list L each element of which is a list of integers and returns a new list whose elements are the sums of the sublists.

1

40 pts.

40 pts.

40 pts.

Problem 6 . Write a function remsmall1(L) that takes a list of integers and modifies it by removing the smallest element. Use list methods.

Write another function remsmall2(L) that returns a new list obtained by removing the smallest element of L, but does not change L.

Problem 7 .

Write a module that contains a function temp convert(temp, scale) where temp in a number and scale is either the string ’C’, indicating temp is given on the Celsius scale, or ’F’, indicating the Fahrenheit scale. The function returns the result of converting temp to the other scale.

Include in the module code so that it can be run as a script that prompts the user for at temperature and (on the next line) the scale (C or F) and prints a message giving the conversion to the other scale.

Problem 8 .

Suppose that “nums.txt” is a text file in the current directory with the following format: some lines of heading information, each of which starts with the character “#”, followed by lines containing varying numbers of integers, separated by whitespace.

Write code to read this file, skip the header and print the sum of all the numbers in the file.

2

EXAM

Exam 1

Math 3430, Fall 2014

Sept. 30, 2014

• Write all of your answers on separate sheets of paper.

You can keep the exam questions when you leave.

You may leave when finished.

• You must show enough work to justify your answers.

approximations (e.g., 2, not 1 .

414).

• This exam has 8 problems. There are 320 points total .

Good luck!

Download