Quiz1 Practice FitBs


These problems are provided to give you optional practice on the FitB style questions that will be on Quiz1.
Practice-FitB1
Fill in the blank so f(x, y) == g(x, y) for all integers x and y.
def f(x, y):
    if x < y:
        return y - x
    else:
        return x - y

# Note: your answer here MAY NOT include a conditional ('if')

def g(x, y):
    return ___________________
Answer:

abs(x - y)


Practice-FitB2
Fill in the blank so f(n) == g(n) for all integers n.
def f(n):
    n = abs(n)
    if n % 10 > 0:
        return False
    n //= 10
    if n % 10 > 0:
        return False
    n //= 10    
    if n % 10 > 0:
        return False
    return True

def g(n):
    n = abs(n)
    return ____________________
Answer:

n % 1000 == 0


Practice-FitB3
Fill in the blank so f(n) == g(n) for all integers n.
def f(n):
    if (n < 0) or (n >= 1000):
        return False
    a = n // 100
    b = n // 10 % 10
    c = n % 10
    return a == b == c

# Note: your answer here MAY NOT include more than one == or != operator.

def g(n):
    if (n < 0) or (n >= 1000):
        return False
    return ____________________
Answer:

n % 111 == 0