Comp 115 - Robots, Games and Problem Solving Spring 2020 Test 3 There are 60 total points. Name: Instructions: • Your answers are due on onCourse at 6:00pm EDT, Tuesday March 31. • You can write your answers using any text processing software you want (Google Doc, Word, etc), but the file you submit on onCourse should be a pdf (so either export, print or download the document as a pdf before submitting). There is a link to a Google Doc template that you can use for your answers (you will need to duplicate the document, I authorized you to view only so that no answers will make it on the public document). • When writing code, please used a fixed-size (also known as monospace) font like Courier New to make the spacing more clear • The exam is open notes, open book, open Internet. You can access any resource that is already out there, but are not allowed to consult other human beings – even strangers on the Internet – while taking this test. • Review your answers carefully and make sure you answered every question. • You do not actually have to write your name on this test, I just didn’t feel like changing the header I use for my tests. 1 Comp 115 - Robots, Games and Problem Solving Spring 2020 Question 1 (10 points). Assume that the instruction mylist = [[1,2,4],3,6,-1,11,12,16,17,21] was just executed. Using only the element access and slicing operator on mylist, construct an expression that would evaluate to the following: a) [11,12,16] b) [3,11,17] c) [12,11] d) [17,6] e) [2,4] Question 2 (8 points). For each of the following pieces of code, write the full sequence of values that would be assigned to each variable if the code was executed. Please write your answer in the form of a table with one column for each variable. Also write what would be printed on screen by each piece of code. (a) x = 1729 if x > 100: grade = "huh?" elif x >= 90: grade = "A" elif x >= 80: grade = "B" elif x >= 70: grade = "C" elif x >= 60: grade = "D" else: grade = "F" print("The grade is", grade) (b) x = 1729 if x > 100: grade = "huh?" if x >= 90: grade = "A" if x >= 80: grade = "B" if x >= 70: grade = "C" if x >= 60: grade = "D" else: grade = "F" print("The grade is", grade) 2 Comp 115 - Robots, Games and Problem Solving Spring 2020 Question 3 (15 points). Draw a stack diagram representing the state of all the function frames at the point in the code indicated by a comment. You can look at the Python documentation to determine what the string method isupper does. Be careful, the variable names were chosen specifically to be a little confusing. def main(): x = "Hello" y = 5 z = weird(x,y) print("in main:", x , y, z) def weird(y,z): if y.isupper(): x = y else: x = z + len(y) # draw stack diagram here (after if statement) print("in weird:", x , y, z) return x if __name__ == "__main__": main() Then determine what would be printed on screen if the entire program was executed. Question 4 (12 points). Write a function called tripleTail that takes one input parameter, assumes that this parameter is a list and returns a list whose elements are the same as in the input list, except that the last three elements of the list are repeated three times. For example, if the function is given the list [1,2,3,4,5] as its input parameter, the function should return [1,2,3,4,5,3,4,5,3,4,5]. If the list contains fewer than 3 elements, all the elements of the list should be repeated three times. For example, if the function is given the list [7,8] as its input parameter, the function should return the list [7,8,7,8,7,8]. To get full marks, you code should use the operator * on lists. Question 5 (15 points). Write a program that asks the user to enter two integers (using two separate input statements) and produces two list: a first list that contains the numbers entered by the user (note that the elements of the list should be int) and the second list contains two boolean values, each of which is True if the corresponding number in the first list is even, and False otherwise. At the end, the program should print both lists. 3