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, exam will pick 8 at random) What is the Linux command to do each of the following? 0.1) See which user you are logged in as: 0.2) Remove a directory: 0.3) Clear the screen: 0.4) Remove a file: 0.5) Change directories: 0.6) See which users are logged in: 0.7) Edit a text file: 0.8) Print current directory: 0.9) View the manual for a command: 0.10) Make a new directory: 0.11) Copy a file: 0.12) Print the name of the computer you are running commands on: 0.13) Move a file or directory: 0.14) List directory contents: 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) top of the entire file system (like C: on Windows, one character): 1.2) wildcard, used to match multiple files (one character): 1.3) shortcut to your home directory (one character): 1.4) auto-complete the command or file name you are typing (one key): 1.5) scroll through the commands you have executed (two keys): 1.6) one directory higher than you are now (two characters): 1.7) run the command just typed (one key): 1.8) current directory (one character): 2) Python keywords (8 points, 1/2 point each, exam will pick 16 at random) For this section answer with the appropriate python keyword(s) 2.1) Function to write to the screen: 2.2) Exit the current loop: 2.3) Function to convert a string to an integer: 2.4) Loop with that iterates through a list: 2.5) Boolean values (two keywords): 2.6) Go back to top of the loop (end the current iteration): 2.7) Determine whether two objects are the same object(not just value): 2.8) Boolean operator, True if either side is True: 2.9) To load a module: 2.10) Handle an exception (2 keywords): 2.11) Function to generate a sequence of numbers: 2.12) Loop with only condition: 2.13) Special value for a variable that has no value: 2.14) Test if something is inside of a list/string/tuple/dictionary: 2.15) Keyword to send a value back from a function: 2.16) Remove from a list by position: 2.17) Text data type: 2.18) Boolean operator, negates: 2.19) Data type for True and False: 2.20) Boolean operator, True only if both sides are True: 2.21) Declare a function: 2.22) Function to get the length of a string: 2.23) Conditional statements (three keywords): 3) Evaluate the Python expression (8 points, 1/2 point each, exam will have 16 similar to these) Assume the following initialization code has just run. Write exactly what Python would print as the result of each expression. peeps = {123: ['last', 'first', 32], 445: ['vast', 'birst', 44], 557: ['gast', 'kirst', 56]} peeps[445][0] = 'ok' peeps[557][1] = 'dokay' msg = 'This is a test. This is a test.' words = msg.split(' ') tot = len(words) tot2 = len(msg) def sum(x): total = 0 for y in x: total += len(y) w = 3 ww = 3.14 www = 4 3.1) 'w' in msg 3.2) '3' == w 3.3) www % 2 3.4) w < ww 3.5) peeps[123][1][0] 3.6) 'T' in msg 3.7) msg[0] 3.8) False and True 3.9) w // 2 3.10) str(5 + 3) 3.11) peeps[123][0] 3.12) sum(words) 3.13) w / 2 3.14) words[0] 3.15) 2 ** w 3.16) len(msg) 3.17) www ** 2 3.18) str('5 + 3') 3.19) False or False 3.20) sum('hello') 3.21) w == ww 3.22) w % 2 4) Short Python (8 points total, 2 points each, exam will have 4 similar to these) 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) add an item 'hello' to words 4.2) print the summation of v[2] over all v in peeps 4.3) set www to be ww times w 4.4) print v[0] for each value v in peeps 4.5) print last letter in msg 4.6) count how many times 'i' occurs in msg, print the result 4.7) set ww to be 3.14159 4.8) print last item in words 5) Python functions (8 points, 4 points each, exam will have 2 similar to these) Create a python function to do each of the following. 5.1) Parameter is a list of numbers, return the summation of the numbers 5.2) Parameter is an integer n, print integers from 1 up to n 5.3) Parameter is a string, return how many times - is in the string 5.4) Parameters a filename f and string s - open f for writing, write string s to it, close file 5.5) Parameters are string s and letter l, return index of first occurrence of l in s (or -1 if not present) 6) Play computer (8 points, 4 points each) 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 = [1, 2, 3, 6, 5, 4] 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 6.2) Base convert algorithm def convertToBase(x, b): ans = '' while x > 0: ans = str(x % b) + ans x = x // b return ans print(converToBase(35, 2)) Write what x is each time it changes, and x%b each time it gives a new digit of the answer x: x % b: Final value printed to screen: