Part 1: CTs


There are 3 parts to this quiz. Part 1: CTs, Part 2: FRQs, Part 3: Bonus CTs. The Bonus CTs are optional.

You may not run any code in Part 1. Once you move to Part 2, you may not return to Part 1. Once you move to Part 3, you may not return to Part 1 or 2.

CT1: Code Tracing [15pts]

Indicate what the following code prints. Place your answers (and nothing else) in the box below. Note that you may not run this code.

def ct1(L):
    M = L
    N = L + [ ]
    M.extend([L.pop(0)] * N[-1])
    N.append(L.pop(-1))
    return M, N
L = [1,2,3]
M, N = ct1(L)
print(L, M, N)
print(L is M, L is N)

CT2: Code Tracing [15pts]

Indicate what the following code prints. Place your answers (and nothing else) in the box below. Note that you may not run this code.

def ct2(s):
    L = s.split('c')
    s = 'd\n'.join(L)
    M = [v.upper() for v in s.splitlines() if v != 'bd']
    return M[0].replace(M[1], 'Z')
print(ct2('abcbca'))

Part 2: Free Response

Once you move to Part 3, you may not return to Part 2.

Free Response: removeLargeValues(L, m) [20pts]

Write the nonmutating function removeLargeValues(L, m) that takes a list L and an integer m and returns a new list M that contains the integers in L, in the same order, that are no larger than m.

The function should ignore values in L that are not integers.

def removeLargeValues(L, m):
    return 42

########################################################
# Test Function
########################################################
def testRemoveLargeValues():
    print('Testing removeLargeValues()...', end='')
    assert(removeLargeValues([1,2,3], 2) == [1,2])
    assert(removeLargeValues([1,2,3,2,3,4,1], 2) == [1,2,2,1])
    assert(removeLargeValues([1,2,3,2,3,4,1], 1) == [1,1])
    assert(removeLargeValues([1,2,3,2,3,4,1], 0) == [ ])
    assert(removeLargeValues([1,'ack',2,3,1.1], 2) == [1,2])
    # Verify that it is nonmutating
    L = [1,2,3]
    removeLargeValues(L, 2)
    assert(L == [1,2,3])
    print('Passed!')

testRemoveLargeValues()

Free Response: makeIncreasing(L) [20pts]

Write the mutating function makeIncreasing(L) that takes a list of values L, which are guaranteed to be of the same type, and mutates L so that each value in L is larger than the value before it.

Specifically, proceed left-to-right through the list, and remove each value if it is not larger than the previous value.

def makeIncreasing(L):
    return 42

########################################################
# Test Function
########################################################
def testMakeIncreasing():
    print('Testing makeIncreasing()...', end='')
    L = [1,2,1,2,4,3,2,1,4,5]
    makeIncreasing(L)
    assert(L == [1,2,4,5])
    L = ['this', 'is', 'zany', 'right?']
    makeIncreasing(L)
    assert(L == ['this', 'zany'])
    # Verify that it returns None:
    assert(makeIncreasing(L) == None)
    print('Passed!')

testMakeIncreasing()

Free Response: checkEquations(equations) [30pts]

Background: For this problem, you will be given a multiline string containing arithmetic equations, one per line, like so:

equations = '''\
10 + 15 = 25
1 - 2 = -1
1 + 1 = 3'''

The equations will always be of the form 'A + B = C' or 'A - B = C', where A, B, and C are always integers. There will always be one space between each term, and always at least one equation.

With this in mind, write the function checkEquations(equations) that takes a a string of equations as just described and returns a list of the incorrect equations (if any) in the same order as they appear in the original string.

def checkEquations(equations):
    return 42

########################################################
# Test Function
########################################################
def testCheckEquations():
    print('Testing checkEquations()...', end='')
    equations = '''\
10 + 15 = 25
1 - 2 = -1
1 + 1 = 3'''
    assert(checkEquations(equations) == ['1 + 1 = 3'])

    equations = '''\
10 + 15 = 25
222 + 0 = -223
1 - 2 = -1
1 + 1 = 3
1 + 1 = 2'''
    assert(checkEquations(equations) == ['222 + 0 = -223', '1 + 1 = 3'])

    equations = '''\
10 + 15 = 25
1 - 2 = -1
1 + 1 = 2'''
    assert(checkEquations(equations) == [ ])

    equations = '''\
120 + 15 = -135'''
    assert(checkEquations(equations) == ['120 + 15 = -135'])

    equations = '''\
120 + 15 = 135'''
    assert(checkEquations(equations) == [ ])
    print('Passed!')

testCheckEquations()

Part 3: Bonus CTs

BonusCT1: Code Tracing [2.5pts]

Indicate what the following code prints. Place your answers (and nothing else) in the box below. Note that you may not run this code.

def bonusCt1(m, n):
    L = list(range(m, n))
    while sum(L) > 0:
        i = 0
        while not L[i]: i += 1
        n += L[i]
        L[i] -= 1
    return n
print(bonusCt1(5, 8))

BonusCT2: Code Tracing [2.5pts]

Indicate what the following code prints. Place your answers (and nothing else) in the box below. Note that you may not run this code.

def bonusCt2(L, M):
    R = [ ]
    while L and M:
        a, b, c, d = L[::len(L)-1] + M[::len(M)-1]
        if (a + b == c + d):
            R.append(int(f'{a}{b}{c}{d}'))
        for k in [0,-1]: L.pop(k); M.pop(k)
    return R
print(bonusCt2([1,2,3,4,3,2,1], [0,3,5,6,1,2]))