## CIT 591 Midterm |
Name ________________________________________ |

- (5 points) Write a function
`letter_count`

to count the number of letters in a string, returning the result as an integer. You may wish to use the`isalpha`

method for characters. Part of the function has been provided for you.def letter_count(s): count = 0

return count

- (5 points) Write a function
`extremes`

that, given a nonempty list of integers, returns the pair( *least*,*greatest*)

is the smallest number in the list, and*least*

is the largest number in the list. You may use Python's built-in functions in your function definition.*greatest*

`def extremes(lst):`

- (5 points) Give an English description of what the following function does.
import random def points(n): directory = {} for i in range(0, n): pname = chr(ord('a') + i) x = 1000.0 * random.random() y = 1000.0 * random.random() directory[pname] = (x, y) return directory

- (15 points) Each of the following pieces of code has an error. Tell what the error is and how to correct it.

`# nums is a list of integers. Add the first and last numbers in the list,`

# and append the result to the list.

nums = nums.append(nums[0] + nums[-1])

`# Delete negative numbers from the list`

for i in lst:

if lst[i] < 0:

lst.remove(lst[i])

`# print the average of the three given numbers a, b, c`

print "The average is " + (a + b + c) / 3

- (5 points)
Given the following function:
def foo(s): d = {} for ch in s: v = d.get(ch, 0) d[ch] = v + 1 return d

what is returned by the call`foo("sassafras")`

?

- (5 points) Given the following function:
def letters(s): ss = [] for ch in s: if ch.isalpha(): ss.append(ch.lower()) return ss

what is returned by the call`letters("1 and 2 and 3")`

?

- (5 points) Given the following function:
def lets(s): return "".join(filter(lambda x: x.isalpha(), list(s)))

what is returned by the call`lets("1 and 2 and 3")`

?

- (5 points) Given the following function:
def f(n): return [foo, points, letters, lets][n]

what is returned by the call`f(1)`

?

- (10 points) Suppose you are defining a class
`Circle`

, and every object of this class must have three values: The`x`

and the`y`

coordinates of the circle's center, and the`radius`

of the circle.- Write the
**constructor**that you would put in this class.

- Use the above constructor to create a circle named
`unitCircle`

with radius = 1 and center at the origin (x = y = 0).

- Write the
- (5 points) What is the purpose of the following code in a Python program?
if __name__ == '__main__': main()

- (5 points) Use a
**list comprehension**to assign to the variable`oddCubes`

the cubes of the odd integers between 0 and100.

- (15 points) Assume that you have written a class
`ListUtils`

to provide a number of operations on lists. This class is in the file`listutils.py`

. In the following three questions you will write a complete class to test one of the functions in`ListUtils`

.

- Write the necessary
`import`

statements and the class header for a class named`ListUtilsTest`

.

- Write a test for the function
`duplicate(lst)`

which is supposed to return a shallow copy of the list given as a parameter.

- Write the statement or statements necessary to perform the tests in the ListUtilsTest class.

- Write the necessary
- (5 points) Given the function
def fiddle(index, lst): index += 1 lst[index] += 1

what will be printed byn, nums = 3, range(1, 6) fiddle(n, nums) print n, nums

- (5 points) In IDLE, what is the value of the variable
`_`

(a single underscore)?

- (5 points) Give
**one**way in which using TDD, Test Driven Design, may improve the**style**(not the correctness) of a program.