15-112 Fall 2013 Quiz2-Retake
* 25 Minutes. No calculators, no notes, no books, no computers. * Show your
work. Circle your answers.
1. Code
Tracing [70 pts == 28 values * 2.5 pts/value] Indicate what this will
print.
For this question only: you may use T for True and F for False, and you may
omit all brackets [ ] in your answers.
Note: each line indicates how many values are printed on that line. To receive
any points for that line, you must have exactly that many values in your answer
(and show your work, of course).
def f1(x):
result = 0
for y in xrange(-x/2, x/3, x/4):
if (y**2 == 1):
result += 1
else:
print y,
return result
print f1(42) # 4 values: ________ _________ __________ _________
def f2(u, v):
count = 0
while (u > v):
u -= v
count += 1
return (u, count)
print f2(123456, 10) # tuple of 2 values ( ________ , _________ )
def f3(x):
a = d = 0
b = 12
while (a <= b):
c = (a+b)/2
d += 1
print c,
if (c == x): return d
elif (c < x): a = c+1
else: b = c-1
print f3(4) # 4 values: ________ _________ __________ _________
def f4(w, x, y, z):
result = 0
for u in xrange(w, x):
result += 100
for v in xrange(y, z):
if (u == v):
print "*",
else:
result += 1
print 10*u+v,
return result
print f4(5, 8, 4, 6) # 7 values: ____ ____ ____ ____ ____ ____ _____
def f5(w, x, y):
result = 0
for u in xrange(w, x):
result += 100
for v in xrange(u, y): # note the use of u here
if (u == v):
print "*",
else:
result += 1
print 10*u+v,
return result
print f5(5, 7, 8) # 6 values: ____ ____ ____ ____ ____ _____
def f6(x, y, z):
counter = 0
while (x%y != z):
for w in xrange(1+counter): x -= w
counter += 1
print x,
return counter
print f6(27, 4, 1) # 5 values: _______
_______ _______ _______ _______
2.
Reasoning Over Code [30 pts == 3 functions * 10
pts/function]
Find arguments for the following functions that make each return True.
def r0(q): value = 0 for x
in xrange(q): if (x == y): value = x return ((100 > q > 0) and (q%10 == 0) and
(q - value == 2)) |
def r1(r): x = y = z = 0 while (r > 0): z += 1 x = 100*x + 10*z + (r%10) r /= 10 return (x == 152431) |
def r2(t): total = 0 for x
in xrange(100):
total += ((x%t) == 0) (t > 0) and (t%13 == 5) and (total == 4))
|
def
rbonus(q): w = 0 while True: (x, y, z) = (0, 0, 1) while (x < w): x = 10*x+z y = y+z*10**(z-1) z = z+1 if ((x + y) == 66666): return (w == q) w += 1
|