CMU 15-112: Fundamentals of Programming and Computer Science
Quiz2b


Quiz2 Version B


See the quiz2 frontmatter.


PART 1 (10 min.)


CT1 (20 points):
Indicate what this code prints.

def ct1(x, y):
    for i in range(x):
        for j in range(i, y):
            if (i + j) % 3 == 1:
               print(j)

        if (i + j > 4):
                break
        elif i < 2:
            print(42)

ct1(3, 5) 





CT2 (20 points):
Indicate what this code prints.

def ct2(a, b, c):
    n = 0
    while n < 123:
        print(n)
        n *= a
        n += c
        b, c = c, b
    return n
print(ct2(10, 8, 6)) 





RC1 (10 pts):
Find an argument for rc1 that makes it return TRUE.
def rc1(x):
    if not isinstance(x, int):
        return False
    elif (x < 1000) or (5000 < x):
        return False

    t = 0
    while x > 0:
        t *= 100
        a = x % 10
        b = (x % 100) - a
        x = x // 100
        t += 10*a + b//10
    return t == 3412




BonusCT1 [+3 points]

This is BONUS. You will not lose points for not answering or incorrectly answering this question. Indicate what this code prints.
def bonusCt1(n):
    (a,b,c) = (0, 1000, 100)
    while (c < 1000):
        for x in range(a, b, c):
          (a,b,c) = (a+1, b-1, c+50)
    return a-n
print(bonusCt1(5)) 



PART 2 (10 min.)

Free Response: nthMarfPrime(n) [50 points]

Write the function nthMarfPrime(n) which returns the nth positive integer which meets the definition of a Marf Prime (a coined term). A number is a Marf Prime if:
For example, 41, 83, 761, and 983 are all Marf Primes, but the following numbers are NOT Marf Primes:
Hint: The smallest Marf Prime is 31
Hint: You must write isPrime (or fasterIsPrime) here.
def testNthMarfPrime():
    print('Testing nthMarfPrime()...', end='')
    assert(nthMarfPrime(0) == 31)
    assert(nthMarfPrime(1) == 41)
    assert(nthMarfPrime(5) == 71)
    assert(nthMarfPrime(9) == 421)
    print('Passed!')