Exam 2

Instructions

• The exam consists of ten (10) questions. Be sure that you have all ten and that they are all legible.

• Read all questions and their instructions thoroughly before you begin. It is worth your time to plan ahead!

• You have also been given three blank pages. Use these sheets for your exam answers. Be sure to write your name on each sheet.

• The exam is worth sixty (60) points. Each question is worth six points.

• Points will be awarded based on your explicit answers. Partial credit will be given where possible, so show all of your work.

• Whenever you are asked to answer or describe "briefly", a one-sentence response should be enough to express the solution.

• Whenever you are asked to "# fill in the blank", you need only write the new code. You do not have to copy the other code from the problem.

• The exam lasts sixty (60) minutes. It is due at 1:45 PM.

Problems

1. When working with files, we use four new bits of Python. Answer briefly these questions about input files.
1. Write the Python statement that opens a text file named grades.txt for input.
2. Write the Python control statement that loops through the text file one line at a time.
3. Write the Python statement that closes the text file when are done reading from it.

2. Write a Python function named list_to_file() that takes as arguments a list and a filename. The function prints the items of the list, one per line, to a file of the given name.

For example,

```     classes = ['CS 1510', 'CS 1520', 'CS 2530']
list_to_file(classes, 'schedule.txt')
```
will write three lines to a file named schedule.txt.

3. Consider this definition:
```     def f(x, y, z):
...
```

List the steps that occur when the statement   var = f(a, 2, int(arg))   is executed.

4. Write a Python function named sum_of_digits() that takes an integer as an argument and returns the sum of its digits. For example:
```     >>> sum_of_digits(1984)
22
```

Recall that we can separate a number into its last digit and the rest of its digits using % and //, respectively.

5. What does this program print?
```     def mystery(x, z, y):
x = x + y
y = y + z
return y * z

x = 1
y = 3
z = 2
x = mystery(z, x, y)
print(x, y, z)
```

6. Write a Python function named unique() that takes a list as an argument and returns a list that contains no duplicates. For example:
```     >>> unique([1, 2, 1, 2, 3, 1, 4, 2, 3, 4])
[1, 2, 3, 4]
```

7. Consider this code:
```     def empty_grid(rows, cols):
grid = []
row  = []
for i in range(cols):
row.append(0)
for i in range(rows):
grid.append(row)
return grid
```

This function does create a grid of independent cells, as expected.

1. What is wrong with this code?
2. Why does it work this way?
3. Suggest a way to fix the code.

8. Write a Python function named get_selection() that takes two numbers as arguments low and high. The function asks the user to enter a number between low and high, inclusive, and returns it. If the user enters an invalid choice, the function keeps asking. For example:
```     >>> choice = get_selection(1, 4)
Enter value between 1 and 4: 6
Sorry, 6 is not in range.
Enter value between 1 and 4: 0
Sorry, 0 is not in range.
Enter value between 1 and 4: 3
>>> print(choice)
3
```

1. What is the relationship between a module at Level n and the module it is connected to on n-1?
2. What is the difference between a concrete step and an abstract step?
3. Why is functional cohesion as valuable property for a module to have?

10. Unix provides a handy little program named wc that prints the number of lines, words, and characters in a text file.
```     > wc card-exercise.txt
13     448    2572 card-exercise.txt
```

Write a Python function named counts that takes a file as an argument (not a filename) and returns a tuple containing the number of lines, words, and characters in the file.

With your function, we can implement wc with this code:

```     lines, words, characters = counts( text_file )
print('{:8d}{:8d}{:8d} {:s}'.format(lines, words, characters, filename))
```

Eugene Wallingford ..... wallingf@cs.uni.edu ..... November 20, 2014