2026-01-29
Max. 100 points
Name:
| Task | Max. | Achieved |
|---|---|---|
| 1 | 50 | |
| 2 | 50 | |
| Sum | 100 |
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.
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)
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.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)