# CIT 591 Midterm

Name ________________________________________
1. (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```

2. (5 points) Write a function` extremes `that, given a nonempty list of integers, returns the pair` (least, greatest)`, where `least` is the smallest number in the list, and `greatest` is the largest number in the list. You may use Python's built-in functions in your function definition.
 ```def extremes(lst): ```

3. (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```

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

1. ```# 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]) ```

2. ```# Delete negative numbers from the list for i in lst:     if lst[i] < 0:         lst.remove(lst[i])```

3. ```# print the average of the three given numbers a, b, c print "The average is " + (a + b + c) / 3```

5. (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") `?

6. (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") `?

7. (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") `?

8. (5 points) Given the following function:
```def f(n):
return [foo, points, letters, lets][n]```
what is returned by the call` f(1) `?

9. (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.
1. Write the constructor that you would put in this class.

2. Use the above constructor to create a circle named` unitCircle `with radius = 1 and center at the origin (x = y = 0).

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

11. (5 points) Use a list comprehension to assign to the variable` oddCubes `the cubes of the odd integers between 0 and100.

12. (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`.

1. Write the necessary` import `statements and the class header for a class named` ListUtilsTest`.

2. Write a test for the function` duplicate(lst) `which is supposed to return a shallow copy of the list given as a parameter.

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

13. (5 points) Given the function
```def fiddle(index, lst):
index += 1
lst[index] += 1```
what will be printed by
```n, nums = 3, range(1, 6)
fiddle(n, nums)
print n, nums```

14. (5 points) In IDLE, what is the value of the variable` _ `(a single underscore)?

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