Quiz4 Practice FitBs


These problems are provided to give you optional practice on the FitB style questions that will be on Quiz4.
Practice-FitB1
Fill in the blank so f(L) == g(L) for all lists L where the values in L are all integers.
def f(L):
    assert(len(L) > 0)
    i = -1
    for j in range(len(L)-1, -1, -1):
        if L[j] <= L[i]:
            i = j
    return i

def g(L):
    assert(len(L) > 0)
    return ___________________
Answer:

L.index(min(L))


Practice-FitB2
Fill in the blank so f(L) == g(L) for all lists L where the values in L are all integers.
def f(L):
    result = True
    for v in range(len(L)):
        result = result and (v in L)
    return result

def g(L):
    return ___________________ == list(range(len(L)))
Answer:

sorted(L)


Practice-FitB3
Fill in the blank so f(L) == g(L) for all lists L where the values in L are all integers.
def f(L):
    assert(0 <= v <= 9 for v in L)
    result = 0
    for v in L:
        result = 10 * result + v
    return result

def g(L):
    assert(0 <= v <= 9 for v in L)
    M = [str(v) for v in L]
    return ___________________
Answer:

int(''.join(M))


Practice-FitB4
Fill in the blank so f(L, M) == g(L, M) for all lists L and M where the values in L are all positive integers. Ignore cases where f crashes.
def f(L, M):
    assert(len(L) == len(M))
    N = [ ]
    for i in range(len(L)):
        N.append((L[i], M[i]))
    for x, y in N:
        if x % y != 0:
            return False
    return True

def g(L, M):
    assert(len(L) == len(M))
    return ___________________
Answer:

sum([L[i] % M[i] for i in range(len(L))]) == 0


Practice-FitB5
Fill in the blank so f(L) == g(L) for all lists L where the values in L are all integers.
def f(L):
    x = sum(L[:2])
    for i in range(2, len(L)):
        if L[i] != x:
            return False
        x += L[i]
    return True

def g(L):
    return False not in [ ___________________ for i in range(2, len(L))]
Answer:

Here are two possible solutions: