15-110 Spring 2011 Quiz 3
20 minutes

SHOW YOUR WORK.  Correct answers without supporting work will not receive credit.
For questions saying “in 10 words or less”, we will only grade the first 10 words you write!

1.       [30pts; 10 pts each] Indicate what each will print:

x = 5
if (x < 10):
    print "x"
if (x < 15):
    print "y"
else:
    print "z"









x = 5
while (x < 10):
    print "in loop:", x
    x += 3
print "after loop:", x

x = 5
for y in range(3):
    x += y
    print "in loop:", x, y
print "after loop:", x, y

 

2.       [40pts; 13 pts each, 1 for breathing] Indicate what each will print:

for row in range(4):
    for col in range(3):
        if ((row % 2) ==
            (col % 2)):
            print "-",
        else:
            print "x",
    print











def f(x,y):
    result = 0
    while (x < y):
        result += x
        x += result
        print x, result
    return result


x = 2
y = 8
print f(x,y)
print x,y

def f(s):
    t = ""
    for i in range(len(s)):
        if (i % 3 == 0):
            t += s[i]
    return t

print f("abcdefg")

 

3.       [10pts]  In the shotgun algorithm, DNA is split into small segments, which are sequenced, and then these sequences are reassembled so the entire original DNA strand is sequenced.  In just a few words, how does the shotgun algorithm know how to place two sequences next to each other in the re-assembly step?

 

4.       [10pts] In just a few words, why does the Jeopardy-playing Watson require so much parallel computing?  Couldn’t it get the same answers without parallel computing?

 

5.       [10 pts; 5 pts each] Indicate what each will print (it is better to reason about these than trace them):

def f(n):
    result = 0
    n = abs(n)
    while (n > 0):
        d = n % 10
        if (d % 2 == 1):
            result = 10*result + d
        n /= 10
    return result

print f(-192837465)


def f(s,t):
    p = ""
    for i in range(min(len(s),len(t))):
        p += s[i] + t[i]
    q = ""
    for c in p:
        if ((c > "1") and (c < "8")):
            q += c
    return int(q)

s="a8b7c6d5e4f3g2h1i0"
t="56ab78cd90ef"
print f(s,t)


 

6.       Bonus/Optional [5 pts]:  What will the following code print?  Show your work!

def f(x,y):

    w = 0

    for z in range(y/2,x/2): w += z/4

    return w

def g():

    for c in "abcdefghijklmnopqrstuvwxyz":

        if f(ord(c)-ord("a"),0) > 0: return c

    return "failed!"

print g()