15-110 Fall 2010 Quiz 6
15 Minutes
* No calculators, no notes, no books, no computers.
* Show your work. Correct answers
without supporting calculations will not receive full credit.
1. Tracing
(30 pts)
Indicate what the following code will print.
A) (15 pts)
a = [1, 2, 3, 4]
x = 9
while (x > a[0]):
x -= a.pop(0)
print x
print a
B) (15 pts)
a1 = [ "a" , "bcd", "efg" ,
"ij" ]
a2 = [ "kl", "m" ,
"nop" , "q" ]
a3 = [ ]
for i in range(len(a1)):
if (len(a1[i]) > len(a2[i])):
a3.append(a1[i])
else:
print a2[i]
print a3
2. Mystery
Method (20 pts)
State in just a few words of plain English what the following function does in
general:
def mystery(a):
# assume a is a list
b = [ ]
for i in range(len(a)):
for j in range(i+1, len(a)):
if ((a[i] == a[j]) and (a[i]
not in b)):
b.append(a[i])
return b
3. (50 pts) Write the function isIdentity(matrix). This function takes a matrix (a rectangular
2d list), and returns True if the matrix is an “identity” matrix and False
otherwise. An Identity Matrix must be a
square matrix (same number of rows as cols), and every value must be zero
except on the top-left to bottom-right diagonal, where the values must be one.
4. Bonus/Optional (5 pts)
State in just a few words of plain English what the following function does in
general:
def mystery(a):
# assume a is a list of non-negative
integers
b = range(len(a))
b.remove(0)
b.append(len(a))
for x in a:
if (x not in b):
return False
b.remove(x)
return (b == [ ])