CMU 15-112: Fundamentals of Programming and Computer Science
Midterm1 version A
Midterm1 Version A
See the midterm1 frontmatter. 80 minutes total..
PART 1
CT1 (8 points):
Indicate what this code prints.
def ct1(s):
r, n = '', 0
for t in s.upper().split(' '):
n += 1
c = chr(ord('A') + n)
r += f'{c}{t.count(c)}'
return r
print(ct1('about baccarat wins!'))
CT2 (8 points):
Indicate what this code prints.
def ct2(L):
M = [ ]
for i in range(len(L)):
for j in range(i):
if (L[i] == L[j]):
M.append(10*i+j)
return M[1:] + M[:1]
print(ct2([2,3,3,2,5,3]))
CT3 (8 points):
Indicate what this code prints.
def ct3(L):
M = sorted(L)
N = L
M.append(L.pop(0))
N.append(M.pop(0))
N = N[:2]
return M, N
L = [3,1,2]
M, N = ct3(L)
print(L, M, N)
CT4 (8 points):
Indicate what this code prints.
from dataclasses import make_dataclass
def ct4():
A = make_dataclass('A', ['x', 's'])
M = [ A(x=3,s='a'),
A(x=1,s='bc'),
A(x=4,s='de'),
A(x=2,s='f') ]
b = A(x=0, s='')
for a in M:
if a.x > b.x:
b.x += a.x
else:
b.s += a.s[0]
return [b.x, b.s]
print(ct4())
Note: The ordering of SA-answers was randomized for each exam, and may not match perfectly.
SA1 (3 points):
Which of these does not set M to a copy of L?
- M = copy.copy(L)
- M = L[:]
- M = L + [ ]
- M = L
- M = list(L)
SA2 (3 points):
2. What is the difference between L.append(v) and L.extend(v)?
- There is no difference
- L.append adds one value and L.extend adds a list of values
- L.append adds to the end of L and L.extends adds to the start of L
- There is no such thing as L.extend
SA3 (3 points):
Fill in the blank with ONE line so that M is equal to L, only with the value 5 nondestructively inserted at index 3 (so L is unchanged).
You may assume len(L)>3:
M = ____________
SA4 (3 points):
Fill in the blank so that T refers to a singleton (length 1) tuple containing just the value 42:
T = _____________
SA5 (3 points):
Fill in the blank with a list comprehension so that M contains the list of the squares of the values in L, which you may assume is a list of integers:
M = ____________
SA6 (3 points):
Each of the following are from our Snake case study. For each, indicate if they are part of the Model, the View, or a Controller:
- timerFired(app)
- takeStep(app)
- drawGameOver(app)
- app.foodPosition
- placeFood(app)
PART 2
Free Response: nthSortOfSquarish(n) [25] points]
Important note: you cannot use strings for this problem but lists are ok.
A number n is sort-of-squarish if all of these are true:
- n is positive
- n contains no zeros
- n is not a perfect square
- the digits of n, when sorted, form a perfect square.
For example, consider 522. The number formed by sorting those digits is 225, and 225 is a perfect square (15**2). Thus, 522 is sort-of-squarish. Since the order of the digits does not matter, 252 is also sort-of-squarish. Note that 225 is a perfect square so is not sort-of-squarish.
Here are the first 10 sort-of-squarish numbers:
[52, 61, 63, 94, 252, 265, 298, 414, 522, 526]
With this in mind, first write isPerfectSquare(n) that takes a non-negative int n and returns True if it is a perfect square (0, 1, 4, 9, 16, etc) and False otherwise.
Next, write the function isSortOfSquarish(n) that takes a possibly-negative int n and returns if n is a sort-of-squarish number.
Then, also write the function nthSortOfSquarish(n) that takes a non-negative int n an returns the nth sort-of-squarish number, so nthSortOfSquarish(0) returns 52.
Hints:
- Remember, do not use strings here, but lists are ok.
- You must write isPerfectSquare(n), isSortOfSquarish(n), and nthSortOfSquarish(n) along with any other helper functions you use.
- In isSortOfSquarish(n) you might want to make a list containing the individual digits in n.
def isPerfectSquare(n):
return 42
def isSortOfSquarish(n):
return 42
def nthSortOfSquarish(n):
return 42
# These test cases are for your use only. You may comment them out if you
# get stuck and need to move on to the next part.
def testIsPerfectSquare():
print('Testing isPerfectSquare(n))...', end='')
assert(isPerfectSquare(4) == True)
assert(isPerfectSquare(9) == True)
assert(isPerfectSquare(10) == False)
assert(isPerfectSquare(225) == True)
assert(isPerfectSquare(1225) == True)
assert(isPerfectSquare(1226) == False)
print('Passed')
def testIsSortOfSquarish():
print('Testing isSortOfSquarish(n))...', end='')
assert(isSortOfSquarish(52) == True)
assert(isSortOfSquarish(16) == False)
assert(isSortOfSquarish(502) == False)
assert(isSortOfSquarish(414) == True)
assert(isSortOfSquarish(5221) == True)
assert(isSortOfSquarish(6221) == False)
assert(isSortOfSquarish(-52) == False)
print('Passed')
def testNthSortOfSquarish():
print('Testing nthSortOfSquarish()...', end='')
assert(nthSortOfSquarish(0) == 52)
assert(nthSortOfSquarish(1) == 61)
assert(nthSortOfSquarish(2) == 63)
assert(nthSortOfSquarish(3) == 94)
assert(nthSortOfSquarish(4) == 252)
assert(nthSortOfSquarish(8) == 522)
print('Passed')
testIsPerfectSquare()
testIsSortOfSquarish()
testNthSortOfSquarish()
Free Response: animation [25] points]
Write an animation where:
- The canvas starts empty.
- Each time the user presses the mouse:
- A new green dot of radius 20 is added, centered on the mouse press, and
- A line is drawn connecting the center of the green dot to the center of the nearest green dot already drawn, if there is such a dot (breaking ties however you wish).
- Each time the user presses the 'r' key: the app resets, so that all the dots and all the lines are deleted.
- Each time the app has not reset after 5 seconds, it automatically resets (as though an 'r' was pressed).
Hint: We recommend that you write a helper function that takes app, cx, and cy, and returns the (x, y) location of the nearest existing dot.
PART 3
BonusCT1 [+2 points]
This is an optional bonus problem. Indicate what this code prints.
def bonusCt1(n):
return eval(str(list(range(n)))[1:-1]
.replace(', ','-(') +
(n-1)*')')
print(bonusCt1(50))
BonusCT2 [+2 points]
This is an optional bonus problem. Indicate what this code prints.
# Note: z() is a helper function for bonusCt2()
# Hint: ord('0') is 48
def z(n):
m = -n/2
while n > 0: m, n = m+n, n-1
return round(2*m)
def bonusCt2(n):
while n < 100: n = round(z(z(n))/n)
s = str(n)
for i in range(1, 12345):
d = chr(int(s[:i]))
if d.isdigit():
return d * int(s[i:])
return 42
print(bonusCt2(2))