CIT 590 Midterm Exam, Fall 2016 Name ______________________________

Please keep all answers short and to the point. Do not add information that is not asked for; you will not gain points, but you may lose points if you get it wrong.

1. (10 points) The following functions are all intended to check whether a string contains any lowercase letters, but at least some of them are wrong. For each function, describe what the function actually does (assuming that the parameter is a string).

 def any_lowercase1(s): for c in s: if c.islower(): return True else: return False def any_lowercase2(s): for 'c' in s: if c.islower(): return 'True' else: return 'False' def any_lowercase3(s): for c in s: flag = c.islower() return flag def any_lowercase4(s): flag = False for c in s: flag = flag or c.islower() return flag def any_lowercase5(s): for c in s: if not c.islower(): return False return True

2. (12 points) In this question you will construct a function to make the computer's move in a game of Pig. Follow the instructions, and write one single statement for each instruction. Any time you put more than one statement in a box, it will be marked wrong. Show indentation.
 Write the header for a method named ```computer_move ```that takes no parameters. Write a statement to print "Computer's turn", preceded by a blank line Set an integer variable named `sum `to zero. Start a loop that will run until `sum `is 17 or larger. Set a variable `roll `to a random integer between 1 and 6, inclusive. (Assume `random `has been imported.) Print out a message saying what the computer has just rolled. Test if the number rolled is a 1 . . . And if it is, return a zero. . . . but if it isn't a 1 . . . Add the number rolled to `sum`. After the loop is finished, print out a message saying how much the computer has gained this turn. Return from the function with the amount gained this turn.

3. (3 points)What words does the acronym "REPL" stand for?

4. (3 points) What words does the acronym "TDD" stand for?

5. (3 points) Python 2 used ASCII to represent characters. What does Python 3 use?

6. (3 points) What operation is represented by the ^ operator?

7. (3 points) When we talk about a "unit test," what is the "unit" being tested?

8. (3 points) What keystroke in IDLE loads your program into the shell window?

9. (10 points) Suppose ```abc = "abcdefg" ```
1. What is `abc[2:4]` ?

2. What is `abc[5:]` ?

3. What is `abc[:5] `?

4. What is `abc[-3 : -1] `?

5. What is `abc * 3` ?

10. (5 points) Suppose `cards = {"ace" : 1, "deuce" : 2, "trey" : 3}`
What does the following print?
```     for i in cards:           print(i) ```
11. (5 points) Write an anonymous ("lambda") function that takes one integer argument and returns twice that number.

12. (5 points) Write an anonymous ("lambda") function that takes one integer argument and returns half that number if it is even, but three times that number if it is odd.

13. (5 points) Write a statement that uses the `format` method to print out a floating point number `x` with two digits after the decimal point.

14. (6 points) Write three lines of code (one line per box) to do the following:
 Open a file named `data.txt` for reading. Read in the entire file as a list of lines. Close the file.

15. (5 points) Suppose:
```    a = [1, 2, [3, 4]]     b = a[:]     a = [5, 6]```
What is the value of `b`?

16. (5 points) Write a single statement to set `squares` to a list of the squares of the numbers 1 through 1000.

17. (6 points) Suppose `small `is defined as:
```     def small(a, b=5):          return a < b ```
What is the result of:
1. ```list(map(small, [1, 2, 3, 4, 5])) ```
2. ```list(filter(small, [1, 2, 3, 4, 5])) ```
3. ```reduce(small, [1, 2, 3, 4, 5]) ```

18. (5 points) Write one line of  Tkinter code to create a button that (1) shows the text `"Click me"` and (2) when clicked, calls the function` clicked()`.

19. (3 points) Name (don't describe, just name) the three methods that can be used to arrange widgets in a Tkinter window.