2026-01-28
Max. 100 points
Name:
| Task | Max. | Achieved |
|---|---|---|
| 1 | 12 | |
| 2 | 24 | |
| 3 | 34 | |
| 4 | 30 | |
| Sum | 100 |
| Statement | True | False |
|---|---|---|
| In Python it is recommended to use five spaces for indentation. | ||
| A function can be considered a named suite of code that can be parameterized. | ||
| Python code can be edited with any text editor. | ||
| In Python, strings are mutable. | ||
| The "ifmain" pattern allows a module to behave differently when it is executed vs. imported. | ||
| The sole purpose of indentation in Python is to make the code easier to read. |
l = ['a', 'b', 'c', ]
print(l.append('d'))
def my_function(n):
return [i**2 for i in range(n)]
print(my_function(4))
def sum(a, b):
return a + b + c
sum(4, 2, 3)
l = [4, 3, 2]
print(l.sort())
class my_class():
def __init__(self, x):
self.x = x
def my_method(self, y):
return (self.x**2 + y**2)**(0.5)
foo = my_class(3)
print(foo.my_method(4))
print({1, 2, 10, 2, 1, 5})
rot13(text: str) -> str which transforms
each alphabetic
character and replaces it with the character appearing 13 characters before
or after it in the alphabet. All other characters are kept as their are.
For example, the character 'A' is replace with 'N' and vice versa. 'B'
is replaced with 'O' and vice versa.rot13('abc') # -> 'nop'
rot13('Python 4tw!') # -> 'Clguba 4gj!'
Use the ord(char)->int
(for example ord('A') -> 65, ord('B') -> 66, ...) and
chr(i:int)->str
(for example chr(65) -> 'A', chr(66) -> 'B')
functions in your implementation.ord and chr
def rot13(text):
"""Return the input string after returning the characters by 13 places."""
result = []
for char in text:
if 'A' <= char <= 'M' or 'a' <= char <= 'm':
num = ord(char) + 13
elif 'N' <= char <= 'Z' or 'n' <= char <= 'z':
num = ord(char) - 13
else:
num = ord(char)
result.append(chr(num))
return "".join(result)
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, provide type annotations for the function,
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)