CS 151, fall 2019 Exam 2 Points - the amount/problem is listed for each problem. There are a total of 39.5 points total Please fill in the following. 0) Name: 1) Python keywords (8 points, 1/2 point each) For this section answer with the appropriate python keyword 1.1) Function to generate a sequence of numbers: range 1.2) Boolean values (two keywords): True, False 1.3) Loop with only condition: while 1.4) Conditional statements (three keywords): if, elif, else 1.5) Handle an exception (2 keywords): try, except 1.6) Text data type: string 1.7) Determine whether two objects are the same object(not just value): is 1.8) Declare a function: def 1.9) Boolean operator, negates: not 1.10) To load a module: import 1.11) Function to write to the screen: print 1.12) Data type for True and False: Boolean 1.13) Boolean operator, True if either side is True: or 1.14) Exit the current loop: break 1.15) Test if something is inside of a list/string/tuple/dictionary: in 1.16) Special value for a variable that has no value: None 2) Evaluate the Python expression (8 points, 1/2 point each) Assume the following initialization code has just run. Write exactly what Python would print as the result of each expression. line = "Hello, my name is HAL." L = line.split(" ") color = "blue" pi = 3.14 prime = 9 d = {"num": pi, "list": L, "tuple": (1, 2, 3, 4)} def total(var): t = 0 for x in var: t = t + x return x def cat(var): s = "" for x in var: s = s + str(x) return s 2.1) 2 < 7 True 2.2) cat("hello") 'hello' 2.3) str(7) + str(6) '76' 2.4) "7" == 7 False 2.5) len([1, 2, 4, 5]) 4 2.6) L[3] 'is' 2.7) 7 <= 7 True 2.8) pi == 3.1 False 2.9) len(L) 5 2.10) 4 + 2 * 3 10 2.11) len("python") 6 2.12) True and False False 2.13) d["tuple"][1] 2 2.14) len(color) 4 2.15) prime == "prime" False 2.16) "0" in d["tuple"] False 3) Short Python (7.5 points, 1.5 points each) Assume the initialization code from problem 2 has just been run. For each of the following, give the python code to complete the task. 3.1) print each letter in line variable on a separate line: for x in line: print(x) # or [print(x) for x in line] 3.2) multiply prime and pi variables, save into variable named c: c = prime * pi 3.3) print first 3 characters in color variable: print(color[:3]) # or for i in range(0, 3): print(color[i]) 3.4) print keys and values in d variable: print(d.keys(), d.values()) # or for k, v in d.items(): print(k, v) # or for k in d: print(k, d[k]) 3.5) print last item in L variable: print(L[-1]) # or print(L[len(L)-1]) 4) Python functions (8 points, 4 points each) Create a python function to do each of the following. 4.1) Parameter is a filename - open, read and print the file def funName(x): f = open(x) s = f.read() f.close() print(s) 4.2) Parameter is a dictionary, changes all values to be None (leave keys as they are) def fun2(d): for k in d: d[k] = None 5) Play computer (8 points, 4 points each) 5.1) Selection Sort def selection_sort(arr): for i in range(len(arr)): # index of the minimum value minx = i for j in range(i+1, len(arr)): if arr[j] < arr[minx]: minx = j # Swap values tmp = arr[minx] arr[minx] = arr[i] arr[i] = tmp L = [5, 9, 3, 2, 4] selection_sort(L) For each value of i, write what L is at the end of the loop inside selection_sort i=0 2, 9, 3, 5, 4 i=1 2, 3, 9, 5, 4 i=2 2, 3, 4, 5, 9 i=3 2, 3, 4, 5, 9 i=4 2, 3, 4, 5, 9 5.2) Play Computer def recurse(i): if i < 10: print(i) recurse(i+2) else: print('Done') recurse(1) Write what is i each time it changes, and everything that is output to the screen. i: 1, 3, 5, 7, 9, 11 screen: 1 3 5 7 9 Done