15-112 Spring 2014 Quiz 2
* 30 minutes. No calculators, no notes, no books, no computers. * SHOW YOUR WORK
1. Quick Answers. [20 pts]
a.
Use a truth table to prove that
(x and (not (y or (not x)))) is
equivalent to (x and (not y)).
b.
Repeat the previous proof, only instead of using a truth table,
use DeMorgan’s Law, along with the rule that
(A and B and A) is equivalent to (A
and B), to simplify the first expression into the second one.
c. Rewrite the
following code so it works equivalently but uses a
while loop instead of a
for loop (you may assume x is a
non-negative integer):
for n in xrange(x, 2*x, 3):
print n
2. Code Tracing [20 pts]: Indicate what each will print:
for z in xrange(5, 15, 5): |
for x in xrange(-5,5): |
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) and (100 > x > y > 0))
z = 0 if (x+y==50) else 123
while (x/10 != y/10):
x -= 10
z += 1
return (x == z == 4)
# Reasoning Over Code (continued)
def g(z):
assert((type(z) == int) and (100 > z > 0))
step = total = 0
while (total < 25):
step += 1
for y in range(1,6,step):
total += y
return (step == z)
4. Free Response: drawCircleDiagram [20 pts]
Write the function f(canvas, x0, y0, x1, y1) that draws the figure to the right in the region bounded by (x0,y0) at the left-top and (x1,y1) at the right-bottom. Assume the smaller circle’s radius is half of the larger circle’s radius. Position your labels using anchors. Don’t forget the horizontal line! |
5.
Free Response: isOddish [20 pts]
We will say that a value is oddish if it is a positive integer where the
sum of its odd digits is greater than the sum of its even digits. With this in
mind, write the function isOddish that takes any value (including non-integers)
and returns True if it is oddish.
6.
Bonus/Optional [2 pts] Indicate what this will print:
def q(x):
total = 1
for x in xrange(x): total += q(x)
return total
print q(4)