CMS Test (Group A)

2026-01-28

Max. 100 points

Name:

Task Max. Achieved
1 12
2 24
3 34
4 30
Sum 100
Grading: >= 88: 1, >=76: 2, >=64 : 3, >50: 4, <=50: 5
  1. Answer the following statements indicating whether they are True or False.
    0-3 correct: 0 points, 4 correct: 4 points, 5 correct: 8 points, 6 correct: 12 points.
    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.
  2. What is the output of the following code snippets? Write exactly what the output of each snippet is if the snippet is the sole content of a Python file. If the output is an error message, it is enough to write “ERROR”. If there is no visible output, write "-".
    4 points for each correct answer.
    1. l = ['a', 'b', 'c', ]
      print(l.append('d'))
      None
    2. def my_function(n):
          return [i**2 for i in range(n)]
      print(my_function(4))
      [0, 1, 4, 9]
    3. def sum(a, b):
          return a + b + c
      sum(4, 2, 3)
      ERROR
    4. l = [4, 3, 2]
      print(l.sort())
      None
    5. 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))
      5
    6. print({1, 2, 10, 2, 1, 5})
      {1, 2, 10, 5}
  3. (34 points)
    Implement 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.
    For example
    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.
    Add a proper docstring in order to receive full points.
    4 points for a proper docstring
    5 points for returning any string
    5 points for returning a string of the same length as the input text
    5 points for correctly rotating each upper case character
    5 points for correctly rotating each lower case character
    5 points for keeping all nonalphabetic signs in the correct place
    5 points for appropriately using 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)
  4. (30 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, provide type annotations for the function, a docstring and at least one doctest.
    3 points for correct signature including type annotations
    3 points for a proper docstring
    3 points for providing a working doctest
    3 points for returning any string
    3 points for returning at least one correct term
    3 points for returning at least one correct term and no incorrect terms
    3 points for returning all correct terms in any order
    3 points for returning the correct terms in the correct order
    3 points for using ", " to separate the terms
    3 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)