1AKIFT POS Test (Group A)

2026-01-29

Max. 100 points

Name:

Task Max. Achieved
1 50
2 50
Sum 100
Grading: >= 88: 1, >=76: 2, >=64 : 3, >50: 4, <=50: 5
  1. (50 points)
    Write a function commands(binary: str) -> str in Python that takes a binary number (zeros and ones) as a string and returns a string of secret actions separated by a comma and a space. The binary string has to be parsed so that for each '1' the appropriate action is added to the output string. The leftmost character is special as it decides if the actions have to be returned in reverse order. Note that it is probably easier to produce a list of actions and turn that list into a string at the end. The actions for each digit are:
    00001 = grunt
    00010 = snort
    00100 = blink
    01000 = smile
    10000 = Reverse the order of the operations
    For example
    commands('00011')  # -> 'snort, grunt'
    commands('00101')  # -> 'blink, grunt'
    commands('10101')  # -> 'grunt, blink'
    commands('11111')  # -> 'grunt, snort, blink, smile'
    Implement the function to always return the correct sequence. To receive full points, also provide a docstring and at least one doctest.
    5 points for the correct function signature
    5 points for a proper docstring
    5 points for providing a working doctest
    5 points for returning any string
    5 points for returning at least one correct term
    5 points for returning at least one correct term and no incorrect terms
    5 points for returning all correct terms in any order
    5 points for returning the correct terms in the correct order
    5 points for using ", " to separate the terms
    5 points for correctly reversing the order
    def commands(binary: str) -> str:
        """
        Return list of commands.
    
        >>> commands('00011')
        'snort, grunt'
        """
        result = []
        if binary[1] == '1':
            result.append('smile')
        if binary[2] == '1':
            result.append('blink')
        if binary[3] == '1':
            result.append('snort')
        if binary[4] == '1':
            result.append('grunt')
        if binary[0] == '1':
            result.reverse()
        return ', '.join(result)
  2. (50 points)
    Implement a function sum_of_multiples(limit: int, multiples: list[int]) -> int that calculates the total sum of all multiples of all values in the given list that are smaller than limit. Only count unique multiples. That means that each multiple should be counted exactly once even if it occurs more often. For example, sum_of_multiples(7, [2, 3]) yields 15 because
    sum_of_multiples(0, [2, 3]) # -> 0
    sum_of_multiples(3, [2, 0, 1]) # -> 3
    sum_of_multiples(2, [2, 3]) # -> 0
    sum_of_multiples(5, [2, 3]) # -> 9
    sum_of_multiples(20, [4, 5]) # -> 4 + 5 + 8 + 10 + 12 + 15 + 16 = 70
    Add a proper docstring, a sample doctest, and type annotations to the function to receive full points.
    5 points for correct type annotations
    5 points for a proper docstring
    5 points for one or more doctests
    5 points for returning 0 if limit <= all multiples
    5 points for returning 0 if not multiples
    5 points for returning the correct sum if len(multiples) == 1
    5 points for returning correct sum if len(multiples) > 1 but all multiples are unique
    5 points for correctly dealing with 0s in multiples
    5 points for using the best data structure (set) for this task
    5 points for returning the correct result in all cases
    def sum_of_multiples(limit: int, multiples: list[int]) -> int:
        """
        Return the sum of all unique multiples less than limit.
    
        >>> sum_of_multiples(5, [2, 3])
        9
        >>> sum_of_multiples(20, [4, 5])
        70
        """
        numbers = set()
        for m in multiples:
            if m == 0:
                continue
            for val in range(m, limit, m):
                numbers.add(val)
        return sum(numbers)