Solutions: You can find the file with solutions for all questions here.

Required questions

Question 1: Falling Factorial

Let's write a function falling, which is a "falling" factorial that takes two arguments, n and k, and returns the product of k consecutive numbers, starting from n and working downwards.

If k is larger than n, only multiply up to n consecutive numbers!

def falling(n, k):
    """Compute the falling factorial of n to depth k.

    >>> falling(6, 3)  # 6 * 5 * 4
    120
    >>> falling(4, 0)
    1
    >>> falling(4, 3)  # 4 * 3 * 2
    24
    >>> falling(4, 1)  # 4
    4
    >>> falling(4, 10)  # 4 * 3 * 2 * 1 # Only n times!!
    24
    """
    if k > n:
        k = n
    total, stop = 1, n-k
    while n > stop:
        total, n = total*n, n-1
    return total

Use OK to test your code:

python3 ok -q falling --local

Question 2: Nonzero

Write a function that takes in a list and returns the first nonzero entry.

def nonzero(lst):
    """ Returns the first nonzero element of a list

    >>> nonzero([1, 2, 3])
    1
    >>> nonzero([0, 1, 2])
    1
    >>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6])
    5
    """
    for i in lst:
        if i != 0:
            return i

Use OK to test your code:

python3 ok -q nonzero --local

Question 3: Hailstone

Complete this question using iteration!

Douglas Hofstadter's Pulitzer-prize-winning book, Gödel, Escher, Bach, poses the following mathematical puzzle:

  1. Pick a positive integer n as the start.
  2. If n is even, divide it by 2.
  3. If n is odd, multiply it by 3 and add 1.
  4. Continue this process until n is 1.

The sequence of values of n is often called a Hailstone sequence, because hailstones also travel up and down in the atmosphere before falling to earth. Write a function that takes a single argument with formal parameter name n, prints out the hailstone sequence starting at n, and returns the number of steps in the sequence:

def hailstone(n):
    """Print the hailstone sequence starting at n and return its
    length.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    s = 1
    print(n)
    while n != 1:
        if n % 2 == 0:
            n = n // 2
        else:
            n = n * 3 + 1
        print(n)
        s = s + 1
    return s

Hailstone sequences can get quite long! Try 27. What's the longest you can find?

Use OK to test your code:

python3 ok -q hailstone --local

Question 4: Classify the elements

Complete the function odd_even that classifies an number as either 'odd' or 'even' and the function classify that takes in a list and applies odd_even to all elements in the list.

def odd_even(x):
    """
    Classify a number as odd or even.
    >>> odd_even(4)
    'even'
    >>> odd_even(3)
    'odd'
    """
    if (x % 2) == 0:
        return 'even'
    else:
        return 'odd'

def classify(s):
    """
    Classify all the elements of a sequence as odd or even
    >>> classify([0, 1, 2, 4])
    ['even', 'odd', 'even', 'even'] 
    """
    return [odd_even(x) for x in s]

Use OK to test your code:

python3 ok -q odd_even --local

Use OK to test your code:

python3 ok -q classify --local

Question 5: Decode

Implement a function decode, which takes in a list of pairs of numbers and returns a list of lists of decoded values.

  • The list contains pairs of the form [sex, age]
  • Sex is an int that is either 0 or 1 and age is an int between 0 and 10
  • Return a list of strings where the Sex gets replaced by "Male" for 0 and "Female" for 1, and age gets replaced by "0-9", "10-19", ..., "90-99", "100+"

See the doctests for examples.

One other thing: your answer can only be one line long. You should make use of list comprehensions and use the helper function!

def decode_helper(pair):
    """
    Optional helper function! Could be useful to turn something like [0, 0] to 'Male 0-9'
    """
    result = ""
    if pair[0] == 0:
        result += "Male "
    else:
        result += "Female "

    if pair[1] == 0:
        result += '0-9'
    elif pair[1] == 10:
        result += '100+'
    else:
        result += str(pair[1]) + '0-' + str(pair[1]) + '9'
    return result

def decode(list_of_sex_age_pairs):
    """
    >>> decode([[0, 0], [1, 1], [1, 10]])
    ['Male 0-9', 'Female 10-19', 'Female 100+']
    >>> decode([[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10]])
    ['Male 0-9', 'Male 10-19', 'Male 20-29', 'Male 30-39', 'Male 40-49', 'Female 50-59', 'Female 60-69', 'Female 70-79', 'Female 80-89', 'Female 90-99', 'Female 100+']
    """ 
    return [decode_helper(pair) for pair in list_of_sex_age_pairs]

Use OK to test your code:

python3 ok -q decode --local