15-112 Fall 2011 Quiz 4
15 Minutes.  No calculators, no notes, no books, no computers.

1.       [60 pts] On the back of this sheet, write the function evalSum, which takes a string of the form “x+y”, with no whitespace, representing the sum of two non-negative integers,  and returns the integer value of that sum.  For example, evalSum("12+3") returns 15 (and not the string "15"), and evalSum("1+234") returns 235 (not "235").  You may import string, but that is not required here. You may not use the eval function (of course).

2.       [10 pts] Very briefly…  (a) what is a magic number?   (b) what is self-documenting code?
 

3.        [10 pts] Consider the following function:
def f(n):
    x = 0
    y = 0
    while (x < n*n):
        x += 5
        y += n**3
    return y

a.       What is the O() number of steps (runtime) of f(n)?

b.      What is the O() of the actual return value (not the number of steps) of f(n)?

4.       [20 pts; 2 pts each] Indicate what each statement or statements will print:

Statement(s):

Prints:

print "ab" * 3 + "c"

 

print "abcdefg"[:3]

 

print "abcdefg"[1:3]

 

print "abcdefg"[-2]

 

for i in xrange(len("abc")): print i

 

for c in "abc": print c

 

for s in "abc\ndef\nghi".splitlines():
    print s

 

s = "hah!"
print s.replace("h","d"), s

 

s = "ab\tc\nd\\\tef"
print s

 

print ("%0.2f%% of %ss\nprefer %5d"
       % (45.678, "lion", 42))

 

 

5.       Bonus/Optional [5pts]:  Given the code below, what will g(40) return?  Show your work!
def f(n): return 5 if (n < 10) else 2*g(n-1)
def g(n): return 2+f(n/2) if (n%2) else 3+f(n/3)