|
First Summer 2006 |
What does this method do?
def what(n1, n2):
return (n1 > n2) * n1 + (n2 > n1) * n2What does this method do?
def what(n):
if n == 0:
return 0
else:
return n + what(n-1)What does this method do?
def what(n, m):
if n == 1:
return m
else:
return what(n-1, m) + mLet's go back to a previous problem and examine it closely:
def what(n):
if n == 0:
return 0
else:
result = n + what(n-1)
return resultLet's ask two questions:
n before the variable result is calculated?
n after the variable result is calculated?
Try it either way:
def what(n):
if n == 0:
return 0
else:
# print n
result = n + what(n-1)
# print n
return resultIn both cases the question, of course, is: why?
What does the following method print?
def play():
j = 0
for i in range(10):
while j < i:
print j
j = j + 1
Now let's make a change, and move j = j + 1 to the left one tab.
def play():
j = 0
for i in range(10):
while j < i:
print j
j = j + 1
Finally, let's move that line one more tab to the left. What gets printed then?
def play():
j = 0
for i in range(10):
while j < i:
print j
j = j + 1Here's a question with which we could and maybe should have started:
def f(x):
return g(x) + (h(x) ** 0.5)
def g(x):
return 4 * h(x)
def h(x):
return x * x + k(x) - 1
def k(x):
return 2 * (x + 1)Given the definitions above, what does this line print:
print k(g(2) + h(2))
Now consider the code fragment below:
i = 2 while i < 10: print i i = i + 2
Which of the following fragments are equivalent to it? (Check all that apply.)
for i in range(2, 10, 2): print i
for i in range(1, 9, 2): print i + 1
i = 2 while i <= 8: i += 2 print i
i = 1 while i < 5: i = i + 1 print 2 * i
for i in range(2, 10): print i print i + 2
Consider the following code fragment when embedded in a complete program:
if x > 3:
if x <= 5:
y = 1
elif x != 6:
y = 2
else:
y = 3
Assume that x has a value of 6 at the beginning of the fragment.
What value does the variable y hold after the fragment gets executed?
Which of the following are infinite loops? Check all that apply:
count = 0 while count > 0: print count count = count + 1
i = 0 while i < 10: print i i = i - 1
i = 1 while i != 10: i = i + 2 print i
x = 0 if x >= 0: print x x = x + 1
x = 0 while x == 0: x = x + 1 print x x = 0
Consider the following two program fragments and assume that x holds an integer.
# fragment 1 | # fragment 2
|
if x == 5: | if x == 5:
x = x + 1 | x = x + 1
else: | if x != 5:
x = 8 | x = 8Which of these statements is true?
Assume that x is an integer variable. Simplify the following boolean expression:
x > 3 and x < 5
Assume that x is an integer variable. Simplify the following boolean expression:
x < 5 and x < 25
Assume that x is an integer variable. Simplify the following boolean expression:
x < 5 or x < 25
Assume that x is an integer variable. Simplify the following boolean expression:
x > 3 or x < 5
Hints: Try to answer the following questions first:
Assume that x is an integer variable. Simplify the following boolean expression:
x > 3 and x < 5