15-112 Fall 2013 Quiz 2

* Note that there were 2 very similar versions of this quiz.  Each is included here.

* 20 minutes.  No calculators, no notes, no books, no computers. 
* SHOW YOUR WORK, CIRCLE YOUR ANSWERS.

1.      Quick Answers.  [20 pts]

a.       With one line of code, draw a circle of radius 5 centered in the region bounded by (50,100) and (250,200).

b.      Explain in just a few words what anchor=SE means when drawing text.

c.       Use DeMorgan’s Law to simplify the expression (not (f(x) or not g(y))) into an equivalent expression that uses fewer operators.

d.      Rewrite the following function so it works equivalently but uses a for loop instead of a while loop (you may assume x is a non-negative integer):
def f(x):
    a = -x
    b = 0
    while (a <= x):
        b += g(a)
        a += 3
    return b
 

2.       Code Tracing  [20 pts]Indicate what each will print:

def f(x,y):
    m = 0
    for z in xrange(2,x,y):
        if (x%z == m):
            print "A",
            m += 1
        elif (y + z > x):
            print "B",
        print (z if (z%3 == 2) else "C"),
    print
f(12, 4)

def g():
    for x in xrange(-1,5,2):
       print x, ":",
       for y in xrange(x, -x, -2):
           print y,
       print
g()


 

3.       Reasoning Over Code  [20 pts]
Find arguments for the following functions that make them return True.  You only need one set of arguments for each function, even if there are multiple correct answers.
    def f(x, y):
        assert((type(x) == int) and (type(y) == int))
        if ((x <= 50) or (y > 25)): z = 3
        elif (x%10 + y%10 > 0): z = 42
        elif (x == y + 40): z = 10
        else: z = 5
        return (z == 2**5/3)


def g(z):
    total = 0
    while (z > 0):
        for y in range(0,z,2):
            total += 1
        z -= 2
    return ((z == 0) and (total == 1+2+3+4+5))
 

4.      Free Response:  isPerfect  [40 pts]
A number n is perfect if it is an integer and it equals the sum of all its factors between 1 (inclusive) and itself (exclusive).  For example, 28 is perfect because the factors of 28 are 1, 2, 4, 7, and 14, and 1 + 2 + 4 + 7 + 14 = 28.  With this in mind, write the function isPerfect(n) that takes a possibly-non-integer value and returns True if it is a perfect number and False otherwise.  Your function may not crash for any value of n.
 

5.  Bonus/Optional  [2 pts each] Indicate what each will print:
    def q():
        def f(x): return x+2*f(x-1) if (x>0) else 1
        def g(x): return f(x/2)*x/2
        x = "f(g(3))"
        (y,f,g) = (eval(x), eval(x)/len(x), eval(x)%5)
        return y+eval(x.replace("(","+").replace(")",""))
    print q()

    def r(x,y,z=0):
        while (x*y > 0): (x,y,z) = (y/(2+x%7),x/(2+y%3),x+y-z)
        return z
    print r(57,15)
 


 

* 20 minutes.  No calculators, no notes, no books, no computers. 
* SHOW YOUR WORK, CIRCLE YOUR ANSWERS.

1.      Quick Answers.  [20 pts]

a.       With one line of code, draw a circle of radius 5 centered in the region bounded by (150,100) and (250,400).

b.      Explain in just a few words what anchor=NE means when drawing text.

c.       Use DeMorgan’s Law to simplify the expression (not (not f(x) and g(y))) into an equivalent expression that uses fewer operators.

d.      Rewrite the following function so it works equivalently but uses a for loop instead of a while loop (you may assume x is a non-negative integer):
def f(x):
    a = -x
    b = 0
    while (a <= x):
        b += g(a)
        a += 4
    return b
 

2.       Code Tracing  [20 pts]Indicate what each will print:

def f(x,y):
    m = 0
    for z in xrange(2,x,y):
        if (x%z == m):
            print "B",
            m += 1
        elif (y + z > x):
            print "C",
        print (z if (z%3 == 2) else "D"),
    print
f(12, 4)

def g():
    for x in xrange(-2,5,3):
       print x, ":",
       for y in xrange(x, -x, -2):
           print y,
       print
g()


 

3.       Reasoning Over Code  [20 pts]
Find arguments for the following functions that make them return True.  You only need one set of arguments for each function, even if there are multiple correct answers.
    def f(x, y):
        assert((type(x) == int) and (type(y) == int))
        if ((x <= 50) or (y > 25)): z = 3
        elif (x%10 + y%10 > 0): z = 42
        elif (x == y + 50): z = 10
        else: z = 5
        return (z == 2**5/3)

def g(z):
    total = 0
    while (z > 0):
        for y in range(0,z,3):
            total += 1
        z -= 3
    return ((z == 0) and (total == 1+2+3+4+5))
 

4.      Free Response:  isPerfect  [40 pts]
A number n is perfect if it is an integer and it equals the sum of all its factors between 1 (inclusive) and itself (exclusive).  For example, 28 is perfect because the factors of 28 are 1, 2, 4, 7, and 14, and 1 + 2 + 4 + 7 + 14 = 28.  With this in mind, write the function isPerfect(n) that takes a possibly-non-integer value and returns True if it is a perfect number and False otherwise.  Your function may not crash for any value of n.
 

5.  Bonus/Optional  [2 pts each] Indicate what each will print:
    def q():
        def f(x): return x+2*f(x-1) if (x>1) else 2
        def g(x): return f(x/2)*x/2
        x = "f(g(3))"
        (y,f,g) = (eval(x), eval(x)/len(x), eval(x)%5)
        return y+eval(x.replace("(","+").replace(")",""))
    print q()

    def r(x,y,z=0):
        while (x*y > 0): (x,y,z) = (y/(2+x%7),x/(2+y%3),x+y-z)
        return z
    print r(57,14)