[ answers in web view ]
 Name: _______________________________ K E Y Student ID: _______________________________

Total points: 70
1. Name the five steps to top-down problem solving as described in the text.
(It's okay if you can't remember the exact wording; the concepts are more important.)
Also: describe each step in your own words, and why it is important! [5]
• Write
• Apprehend
• Design
• Execute
• Scrutinize
2. Name the five program control/flow abstractions we talked about in class. For each one, name a Python language construct which implements the abstraction. (There is one abstraction which we are not covering in Python; you may write "N/A" for the Python for that one.) [5]
• Sequence (newlines)
• Selection (if, elif, else)
• Repetition (while, for)
• Composition (functions)
• Parallelism (N/A)
3. Write "yes" or "no" next to each of the following six strings to indicate whether it can be used as a valid name for a user-defined Python variable. If "no", indicate why. [5]

python -- yes
in -- no (reserved)
If -- yes
my.Apples -- no (punctuation)
____ (4 underscores) -- yes
4. Use the Python range() function to create the following list.
(There are several possible correct answers.)
(Don't worry about Py3's list/iterator issue; just use range().) [2]
[7, 3, -1, -5]
range(7,-4,-9), or ..., up to range(7,-4,-6)
5. Describe the bug in this Python loop, and fix it: [3]
```countdown = 10		# Count from 10 down to 1

while countdown > 0:

print(countdown, end=' ')

countdown - 1

print("Blast off!!")
```
countdown is never decremented: change "- 1" to "-= 1".
6. The following is meant to be an interactive gradebook that converts from a percentage into a letter grade: 95 and above is A+, 85-95 is A, 80-85 is A-, and everything below is fail (it's a really tough program!). The program should prompt the user 100 times for grades. Unfortunately, the program was written by a terrible coder (me!), so there are numerous bugs in the code. Find all the bugs and fix them. [8]

```
For i : range(1 .. 100)

grade == input('What\'s the grade percentage? ')

if grade > 95

print("That's an A+!')

if 85 > grade > 95:

print("""An A is really good!""")

elsif 80 < grade:

print( You got an A-! )

else grade < 80:

print("Sorry, you need an A- to pass!")
```
(1) For → for
(2) i : → i in
(3) range(1 .. 100) → range(100) (not range(1,100), either)
(4) Colon (:) at end of first line
(5) grade == → grade =
(6) input( → int(input( (convert to int)
(7) grade > 95 → grade >= 95 (logic error: 95 and above)
(8) Colon (:) at end of line with 95
(9) Mismatched quotes in "That's an A+!"
(10) if 85 → elif 85
(11) 85 > grade > 95 → 85 <= grade < 95 (logic error)
(12) elsif → elif
(13) You got an A-! → (needs to be in quotes)
(14) Delete: grade < 80
(15) Last print() statement should be indented

The apostrophe in "What's" is okay; triple-quotes are okay; and the condition 80 < grade is also okay.

Corrected code:
```for i in range(100):
grade = int(input('What\'s the grade percentage? '))
if grade > 95:
print("That's an A+!")
elif grade > 85:
print("""An A is really good!""")
elif grade > 80:
print("You got an A-!")
else:
print("Sorry, you need an A- to pass!")
```

7. Evaluate each of the following Python expressions exactly as given, or if it produces an error, describe the error. Assume each expression is independent of the others, and is the only command in a new Python session. [15]

1. list(range(2)) [0, 1]
2. list(range(2,-2)) [] (empty list)
3. list(range(2,-2,-2)) [2, 0]
4. 5 % 13 5
5. 3 ** 2 < 8 and 5 % 0 == 0 False
6. 2 - 2 * - 2 ** 2 10
7. 0.8 + 0.1 + 0.1 = 1.0 SyntaxError: can't assign
8. '7' + '3' '73'
9. "%04.1f" % 2 "02.0"
10. "My name is %s" % Joe NameError: Joe
11. "Name: %s, ID: %04d" % 'Bob', 173 TypeError: not enough args
12. "03.0f" % 2 TypeError (not a format string)
13. ord('E') - ord('B') 3
14. 'E' - 'B' TypeError: can't subtract strings
15. 'pi' + 2*'z' + 'a' 'pizza'

8. Describe and contrast the roles of the producer and the director in a team. Describe specific examples of what these roles might look like for a particular kind of task, e.g., developing software, building a bridge, making a movie, etc. [6]
Producer is a managerial position -- keeping people on task and on schedule, making sure everyone knows what they're supposed to be doing, facilitating communication amongst team members. Practical, realist, engineering-type.
Director has the vision and concept -- a technical director knows the design and code better than anyone else; an artistic director controls the look and feel of the movie; an architect designs the building.
9. Compare and contrast static typing versus dynamic typing, including pros/cons. [4]
(The key here is type: not whether the values of the variables can change, but whether their types can change.)
Static typing: variables cannot change type. Must declare type of variable and initialize value. Compiler enforces correct type.
Dynamic typing: can change type. More flexible, but usually we don't want variables to change type.

10. Describe and contrast the requirements document and the design specification for a software project. [5]
Requirements are agreed upon by the client and the design team. They form a written, formal definition of what the client is asking for; they define what it means for the project to "succeed". Ideally, the client would not change the requirements once the software team has started work.
Design specifications are agreed upon by the design team and the implementation team. They form a written, formal definition of the architecture of the software project -- its components, top-down design, etc. It may contain pseudocode, diagrams, sketches, etc.

11. Newton's method for computing square roots is an iterative method which starts from an initial guess of the square root and gets closer to the true square root with each step, according to the following algorithm:
Let x be the number we wish to take the square root of.
Let r be our current guess for the value of the square root.
Calculate our next guess via (r + x/r)/2.
Repeat until the guesses don't change by more than, say, 0.0001 (our desired level of precision).
Your task is to code a Python function, called newton(x), that uses Newton's method to calculate and return the square root of x.
1. Write a docstring for this function, including detailed pre-condition and post-condition. [4]
2. Write Python code implementing this function. Use x/2 as the initial guess for the square root. Obviously, you may not use math.sqrt for this! (It turns out that math.sqrt does use a similar algorithm -- Newton's method converges to the right answer quite quickly!) [8]

Comments and pseudocode are not required but may earn you partial credit if they show good design thinking. Little credit will be given for uncommented incorrect code. Note that your code is not required to print() anything on the screen or to input() anything from the console.
See newton.py.