Don't like this style? Click here to change it! blue.css
#Practice Test | |
#Problem 1: | |
x = True | |
y = False | |
z = True | |
w = z and not x or not y | |
#What value is assigned to w? | |
#solution: | |
w == True | |
#Problem 2: | |
def f(y): | |
return y+4 | |
assert f( f( f( f(1)))) == ----- | |
#what value goes at -----? | |
#solution: 17 | |
#Problem 3: | |
banana = "dole" | |
fruit = "banana" | |
fruit = banana | |
banana = fruit | |
print(banana, fruit) | |
#What gets displayed? | |
#solution: | |
dole dole | |
#Problem 4: | |
total = 0 | |
for value in [3, 5, 2, 7]: | |
total = total + value + 3 | |
print(total) | |
#What will be printed to the screen? | |
#solution: | |
6 | |
14 | |
19 | |
29 | |
#Problem 5: | |
total = 0 | |
for i in range(4, 80, 4): | |
total = i | |
print(i) | |
#What will be displayed? | |
#solution | |
76 | |
#Problem 6: | |
def fork(a, b): | |
if (a > b): | |
a = a + b | |
else: | |
a = a - b | |
output = fork(10, 12) | |
print(output) | |
#What will be displayed? | |
#solution (Tricky): | |
None | |
#Problem 7: | |
for a in [3, 6, 1, 2]: | |
for b in range(1, 3): | |
print(a,b) | |
#What will be displayed? | |
#solution: | |
3 1 | |
3 2 | |
6 1 | |
6 2 | |
1 1 | |
1 2 | |
2 1 | |
2 2 | |
#Problem 8: | |
g = 77 | |
if x <= 100: | |
grade = "A" | |
elif x < 90: | |
grade = "B" | |
elif x < 80: | |
grade = "C" | |
else: | |
grade = "D" | |
print(grade) | |
#What will be displayed? | |
# technical solution is a Syntax Error 'x' is not defined (I made a typo) | |
# implied solution: | |
A | |
#Problem 9: | |
for a in range(3): | |
for b in range(4): | |
print(a, b) | |
#What will be displayed? | |
#solution: | |
0 0 | |
0 1 | |
0 2 | |
0 3 | |
1 0 | |
1 1 | |
1 2 | |
1 3 | |
2 0 | |
2 1 | |
2 2 | |
2 3 | |
#Problem 10: | |
#Write code which computes and prints the product: | |
# 2 * 5 * 8 * 11 * 14 * ... * 905 | |
#solution: | |
product = 1 | |
for i in range(2, 906, 3): | |
product *= i | |
print(product) | |
#Problem 11: | |
# Write a function which takes in non-negative p | |
# and prints "*5" if p is a positive multiple of 5 | |
# prints "not *5" if p is positive but not a positive multiple of 5 | |
# and prints "ZERO" if p is 0. | |
#solution: | |
def myfunc(p): | |
if p == 0: | |
print("ZERO") | |
elif p % 5 == 0: | |
print("*5") | |
else: | |
print("not *5") | |
#Problem 12: | |
#Write a function which computes the average of the first n positive numbers | |
#solution1: | |
def avg(n): | |
total = 0 | |
for i in range(1, n+1): | |
total += i | |
return total / n | |
#solution2: | |
def avg(n): | |
return sum(range(1, n+1))/n | |
#solution3: | |
def avg(n): | |
total = 0 | |
i = 1 | |
while (i <= n): | |
total += i | |
i += 1 | |
return total / n |
I'm going to going to put up a randomized python problem on the board. Work in a team of 1-4 people. When you have a solution write it in chalk on a board. First team to get a working solution on the board gets 1 point.
Now we start a 4 minute timer. During that time teams can try to come up with a shorter solution (tweetable is best). The shortest solution gets 2 points.
If there is a particularly interesting, clever, or funny answer I reserve the right to give that team an extra point. So don't give up when another team gets an answer.
At the end of class whichever team has the most points gets a 1 hint pass each. They can use this pass during the test on Wednesday to get a reasonable hint from me, or a 2% bonus if they never use it.
Good Luck!
Here is the link: Code Golf
Mansion Apartment Shack House! There is a game called MASH which lets you know who you're going to marry (in theory). Implement a basic version as practice for the test.