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 with 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): continue
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(' ') : ["Hello,", "my", "name", "is", "HAL."]
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: False
2.2) cat(d['tuple']): "012"
2.3) 3 <= 3: True
2.4) prime == 'prime': False
2.5) 'n' in color: True
2.6) int('7') + int('6'): 13
2.7) color * 3: "orangeorangeorange"
2.8) prime // 2: 3 (// means integer division drop the decimal)
2.9) not False: True
2.10) 3 < 3: False
2.11) line[0]: H
2.12) 4 + 2 * 3: 10
2.13) '0' in d['tuple']: False
2.14) len(color): 6
2.15) 7 + 6: 13
2.16) prime % 2: 1 (remainder of 1)
2.17) pi == 3.1: False
2.18) str(7) + str(6): "76"
2.19) False or True: True
2.20) len('hi there'): 8
2.21) '7' + '6': "76"
2.22) cat('hello'): "hello"
2.23) 4 * 2 + 3: 11
2.24) 3 == 3: True
2.25) total(range(0, 4)): sum of all of positive nubmers from 1 to n: n(n+1)/2 = 6
2.26) True and False: False
2.27) L[0]: "Hello,"
2.28) 'int("7")' + 'int("6")': 'int("7")int("6")'
2.29) d['tuple'][1]: 1
2.30) 3 != 3: False
2.31) prime ** 2: 49
2.32) len(L): 5
2.33) len([1, 2, 4]):3
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: pi = pi/2
3.2) print keys in d variable: [print(key) for key in d]
for key in d:
print(key)
3.3) print first item in L variable: print(L[0])
3.4) print total of 10+11+12+...+20:
print(sum(range(10,21)))
The sum of 1-20 minus sum of 1-9
print(20(20-1)/2 - 9(9-1)/2)
3.5) add pi and prime variables, save into variable named y: y = pi + prime
3.6) print each letter in line variable on a separate line: [print(x) for x in line]
for x in line:
print(x)
3.7) print first 3 characters in color variable:
# Each on a seperate line
[print(x) for x in color[:3]]
# Print on one line
print(color[:3])
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 n items in the list one
per line
def print_n_items(li, n):
[print(x) for x in li[:n]]
4.2) Parameter is a dictionary, changes all values to be None (leave keys as they are)
def null_dictionary(d):
for key in d:
d[key] = None
4.3) Parameter is a list of numbers, returns the minimum assume list is not
sorted, assume the list contains atleast 1 element.
def get_min(li):
minimum = li[0]
for x in li:
if x < minimum:
minimum = x
return minimum
4.4) Parameter is a filename - open, read and print the file
def print_file(path):
try:
fd = open(path)
print(fd.read())
except FileNotFoundError:
print('File not found')
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
L = [2, 3, 9, 7]
i=1
L = [2, 3, 9, 7]
i=2
L = [2, 3, 7, 9]
i=3
L = [2, 3, 7, 9]
5.2) Play Computer
def countdown(i):
print(i)
if i > 0:
# originally the return statement was missing here
# If there was no return statement the function would have returned None for any positive number
return countdown(i//2)
else:
return i
L = [3, 1, 8]
L[0] = countdown(L[0])
L[2] = countdown(L[2])
L:
[0, 1, 8]
[0, 1, 0]
i: 3 1 0 8 4 2 1 0
screen:
3
1
0
8
4
2
1
0