CS 151, fall 2019 Exam 2 Grading - unless stated otherwise each part is worth 1 point. There are a total of __ points. Please fill in the following. 0) Name: 1) Python keywords (8 points, 1/2 point each, around 16 on the exam) For this section answer with the appropriate python keyword 1.1) Data type for True and False: 1.2) Loop with only condition: 1.3) Test if something is inside of a list/string/tuple/dictionary: 1.4) Function to write to the screen: 1.5) Function to convert a string to an integer: 1.6) Boolean values (two keywords): 1.7) Conditional statements (three keywords): 1.8) Boolean operator, True only if both sides are True: 1.9) To load a module: 1.10) Special value for a variable that has no value: 1.11) Exit the current loop: 1.12) Remove from a list by position: 1.13) Loop that iterates through a list: 1.14) Keyword to send a value back from a function: 1.15) Declare a function: 1.16) Go back to top of the loop (end the current iteration): 1.17) Function to generate a sequence of numbers: 1.18) Function to get the length of a string: 1.19) For using variable from main program inside a function: 1.20) End the current loop: 1.21) Exception handline (5 keywords): 1.22) Determine whether two objects are the same object(not just value): 1.23) Text data type: 1.24) Boolean operator, True if either side is True: 1.25) Boolean operator, negates: 2) Evaluate the Python expression (8 points, 1/2 point each, around 16 on the exam) 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 = 'orange' pi = 3.14 prime = 7 d = {'num': pi, 'list': L, 'tuple': (0, 1, 2)} 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) 'N' in color 2.2) cat(d['tuple']) 2.3) 3 <= 3 2.4) prime == 'prime' 2.5) 'n' in color 2.6) int('7') + int('6') 2.7) color * 3 2.8) prime // 2 2.9) not False 2.10) 3 < 3 2.11) line[0] 2.12) 4 + 2 * 3 2.13) '0' in d['tuple'] 2.14) len(color) 2.15) 7 + 6 2.16) prime % 2 2.17) pi == 3.1 2.18) str(7) + str(6) 2.19) False or True 2.20) len('hi there') 2.21) '7' + '6' 2.22) cat('hello') 2.23) 4 * 2 + 3 2.24) 3 == 3 2.25) total(range(0, 4)) 2.26) True and False 2.27) L[0] 2.28) 'int("7")' + 'int("6")' 2.29) d['tuple'][1] 2.30) 3 != 3 2.31) prime ** 2 2.32) len(L) 2.33) len([1, 2, 4]) 3) Short Python (1.5 points each, around 8 points total on the exam) 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) set pi to be itself divided by 2: 3.2) print keys in d variable: 3.3) print first item in L variable: 3.4) print total of 10+11+12+...+20: 3.5) add pi and prime variables, save into variable named y: 3.6) print each letter in line variable on a separate line: 3.7) print first 3 characters in color variable: 4) Python functions (8 points total on the exam, 2-3 problems) Create a python function to do each of the following. Note - on the test there will be two. 4.1) Parameters are a list and an integer n, print first items in the list 4.2) Parameter is a dictionary, changes all values to be None (leave keys as they are) 4.3) Parameter is a list of numbers, returns the minimum 4.4) Parameter is a filename - open, read and print the file 5) Play computer (8 points total on the exam, 2-3 problems) 5.1) Selection Sort def selection_sort(arr): for i in range(len(arr)): # index of the minimum index 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 = [7, 3, 9, 2] selection_sort(L) For each value of i, write what L is at the end of the loop inside selection_sort i=0 i=1 i=2 i=3 5.2) Play Computer def countdown(i): print(i) if i > 0: countdown(i//2) else: return i L = [3, 1, 8] L[0] = countdown(L[0]) L[2] = countdown(L[2]) L: i: screen: