CS 151, fall 2019 Exam 3 Points - the amount/problem is listed for each problem. There are a total of 52 points total (4 linux commands, 4 getting around terminal, 8 python keywords, 8 python expressions, 8 short python code, 8 python functions, 8 play computer algorithms, 4 recursive function) Please fill in the following. 0) Name: 0) Commands (1/2 point each, 4 points total) What is the Linux command to do each of the following? 0.1) Move a file or directory: 0.2) Delete a file: 0.3) Print current directory: 0.4) List directory contents: 0.5) Edit a text file: 0.6) Make a new directory: 0.7) Change directories: 0.8) Copy a file: 1) Getting Around in the Terminal (1/2 point each, 4 points total) What is the correct thing to type for each of the following in the Linux terminal? 1.1) wildcard, used to match multiple files (one character): 1.2) one directory higher than you are now (two characters): 1.3) run the command just typed (one key): 1.4) current directory (one character): 1.5) scroll through the commands you have executed (two keys): 1.6) auto-complete the command or file name you are typing (one key): 1.7) top of the entire file system (like C: on Windows, one character): 1.8) shortcut to your home directory (one character): 2) Python keywords (8 points, 1/2 point each) For this section answer with the appropriate python keyword(s) 2.1) Test if something is inside of a list/string/tuple/dictionary: 2.2) Special value for a variable that has no value: 2.3) Function to convert a string to an integer: 2.4) Data type for True and False: 2.5) Boolean operator, True only if both sides are True: 2.6) Determine whether two objects are the same object(not just value): 2.7) Function to generate a sequence of numbers: 2.8) Text data type: 2.9) Loop that iterates through a list: 2.10) Keyword to send a value back from a function: 2.11) To load a module: 2.12) Function to get the length of a string: 2.13) Conditional statements (three keywords): 2.14) Declare a function: 2.15) Boolean operator, True if either side is True: 2.16) Go back to top of the loop (end the current iteration): 3) 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. peeps = {'ha': ['last', 'first', 'ok'], 'bo': ['vast', 'birst', 'the'], 'lu': ['gast', 'kirst', 'then']} peeps['bo'][0] = 'ok' peeps['lu'][1] = 'dokay' msg = 'Hello world. Goodbye now.' words = msg.split(' ') tot = len(words) tot2 = len(msg) def prod(x): total = 1 for y in x: total *= len(y) return total w = 3 ww = -2.7 www = 3**2 3.1) False or False 3.2) False and True 3.3) prod(peeps['ha']) 3.4) w // 2 3.5) w < ww 3.6) str('2 + 1') 3.7) w / 2 3.8) len(msg) 3.9) 2 ** w 3.10) '3' == w 3.11) words[0] 3.12) str(2 + 1) 3.13) 'g' in msg 3.14) 'w' in msg 3.15) w % 2 3.16) peeps['bo'][0] 4) Short Python (2 points each, 8 points total) Assume the initialization code from problem 2 has just been run. For each of the following, give the python code to complete the task. 4.1) print how many items are in words 4.2) for each /value/ v in peeps, print v[0] 4.3) count how many times 'i' occurs in msg, print the result 4.4) set www to be ww times w 5) Python functions (4 points each, 8 points total, 4 points each) Create a python function to do each of the following. 5.1) Parameter is an integer n, print integers from n down to 1 5.2) Parameter is a string, return how many times the - character is in the string 6) Play computer (8 points, 4 points each) 6.1) Base convert algorithm def convertToBase(x, b): ans = '' while x > 0: ans = str(x % b) + ans x = x // b return ans print(converToBase(41, 2)) Write what x is each time it changes, and x%b each time it gives a new bit of the answer x: x % b: Final value printed to screen: 6.1) Selection Sort algorithm def selection_sort(arr): for i in range(len(arr)-1): # 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, 4, 3, 6, 2, 7] 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 i=4