15-112 Fall 2013 Quiz 7 Part 1 (in lecture) (Part 2 will be in recitation)
* 23 Minutes. No calculators, no notes, no books, no computers. * Show your
work. Circle your answers.
1.
Code Tracing [12 pts] Indicate what this will print.
# prints 4 values
def f1(g,h,x): return lambda y: g(h(x+y))
def f2(x): return x-10
f3 = lambda x: 100*x
f4 = f1(f2, f3, 3)
try: print f1.__name__, f4.__name__, f4(4), f4(4)(4), f4
except: print f1(lambda z: z+2, lambda z: z*2, 1)(4)
# prints a list of 4 values
print map(lambda x: x*3, map(lambda x: x%4, range(2,6)))
def f(x):
def g(y):
return reduce(lambda r, s: int(str(r)+str(s)), range(x,y))
return g
f1 = f(1)
f2 = f(2)
print f1(3), f2(3), f1(4), f2(4) # prints 4 values
2. Free
Response: myReduce [18 pts]
Write the function myReduce so the following test code works. Note
that myReduce works exactly like reduce, and you may not call reduce in your
solution.
assert(myReduce(operator.add,
range(3,6)) == (3+4+5))
assert(myReduce(operator.mul, range(3,6), 1) == (3*4*5))
3. Free
Response: Q and PQ [20 pts]
Write the Q and PQ classes so the following test code works. Note
that your PQ class may only have one method -- remove – and it does not have to
be very efficient. Also, note that the class Q implements a "Queue", so
Q.remove will return the least-recently-added value (so this is first-in
first-out). Also, the class PQ implements a "Priority Queue", so PQ.remove will
return the smallest value regardless of when it was added.
q = Q()
assert(str(q) == "<Q of size 0>")
q.add(5)
q.add(3)
assert(str(q) == "<Q of size 2>")
assert(q.remove() == 5) # first-in, first-out!
assert(str(q) == "<Q of size 1>")
assert(q.remove() == 3)
assert(str(q) == "<Q of size 0>")
q1 = Q()
q1.add(42)
q2 = Q()
q2.add(42)
q3 = Q()
q3.add(99)
assert(q1 == q2)
assert(q1 != q3)
pq = PQ()
assert(type(pq) == PQ)
assert(isinstance(pq, Q))
pq.add(4)
pq.add(1)
pq.add(2)
pq.add(3)
assert(str(pq) == "<PQ of size 4>")
assert(pq.remove() == 1)
assert(pq.remove() == 2)
assert(str(pq) == "<PQ of size 2>")
4.
Optional/Bonus: Tracing [2.5 pts]: Indicate what this
will print.
def f(n):
try: return (reduce(operator.mul,
map(lambda i: bool(n%i),
range(2,n))))
except: return (n==2)
for n in xrange(30):
if (f(n)): print n,
15-112Fall 2013 Quiz 7 Part 2 (in recitation)
* 15 Minutes. No calculators, no notes, no books, no computers. * Show your
work. Circle your answers.
Very Short Answers (12 pts; 3 pts each)
1.
Write a single timerFired function that calls f() twice per second and also
calls g() 5 times per second.
2.
What would the user experience if we omitted all the calls to root.bind in our
Snake implementation?
3.
What would the user experience if we omitted the call to canvas.delete(ALL) in
our Snake implementation?
4.
How did our BoardGame implementation manage the scope of canvas (so it was
visible in keyPressed, mousePressed, timerFired, and redrawAll) without using
globals or closures?
Tetris Multiple Choice (8 pts; 2 pts each)
These questions all refer to our implementation of Tetris
5.
Where do we use 3d lists?
a. canvas.data.tetrisPieceColors
b. canvas.data.tetrisPieces
c. canvas.data.board
d. None of the above
6.
Which of the following will move a piece to the right? (be careful…)
a. moveFallingPiece(+1,0)
b. moveFallingPiece(-1,0)
c. moveFallingPiece(0,+1)
d. moveFallingPiece(0,-1)
7.
Where do we call placeFallingPiece?
a. keyPressed()
b. timerFired()
c. drawGame()
d. init()
8.
Which function is most similar in structure to drawFallingPiece()?
a. fallingPieceIsLegal()
b. moveFallingPiece()
c. rotateFallingPiece()
d. newFallingPiece()
9.
Free Response: Half Snake [30 pts]
Starting from the snake8.py file at the end of the Snake Tutorial, add the
following feature: when the user presses ‘h’, the snake is cut in half. The
front half of the snake, from the head to its middle, is unchanged. But the
back half of the snake disappears. You may assume the snake is of even length.
Only include the new code required to do this, and when applicable, indicate
the function to which the new code should be added.
10.
Optional/Bonus: Tracing [2.5 pts]: Indicate what this
will print.
t = [0]
def ss(s):
t[0] += 1
return str(t[0]) + ":" + s
class C(object):
def s(self, s): return "C%s"%ss(s)
class D(C):
def s(self, s): return super(D,self).s(s[:-1])
class E(D):
def s(self, s): return ss("E%s:%s"%(super(E,self).s(s),
super(D,self).s(s)))
print E().s("ab")