1AKIFT POS Test (Group B)

2023-01-11

Max. 32 points

Name:

Task Max. Achieved
1 6
2 10
3 6
4 4
5 6
Sum 32
Grading: <16: 5, 16-20: 4, 21-24: 3, 25-28: 2, 29-32: 1
  1. Answer the following statements indicating whether they are True or False.
    0-6 correct: 0 points, 7 correct: 1 point, 8 correct: 2 points, 9 correct: 3 points, 10 correct: 4 points, 11 correct: 5 points, 12 correct: 6 points.
    Statement True False
    In Python, the def keyword is used to define a function.
    In Python it is recommended to use four spaces for indentation.
    The "ifmain" pattern allows a module to behave differently when it is executed vs. imported.
    Python code cannot be edited with a regular text editor. A special editor is required.
    In Python, it is possible to iterate over sequences.
    A Python dictionary is particularly useful when frequently testing if a value exists in the data structure.
    Dictionaries in Python are immutable.
    Python sets are mutable.
    Python tuples are immutable.
    Python sets support the intersection, difference and union operations.
    Doctests are a great way to illustrate how code is supposed to be used.
    Comments are the same as docstrings.
  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 "-".
    2 points for each correct answer.
    1. def sum(a, b, c):
          return a + b + c
      sum(4, 2, 3)
      -
    2. def circumference(length, width):
          return 2 * length + 2 * width
      print(circumference(5, 2))
      14
    3. s = 'Hello World!'
      s.lower()
      print(s)
      Hello World!
    4. l = [4, 2, 3, 1]
      print(l.append(0))
      None
    5. def fun(a, b=5, c):
          return a + b + c
      print(fun(10, 5))
      Error
  3. Implement a function find_max(iterable) that returns the maximum value of an iterable. It can be assumed that the iterable contains at least one value and that all values in the iterable are numeric. Don't forget to add a proper docstring. You are not allowed to use the built in max(.) function. (6 points)
    1 point for the correct function signature
    1 point for a proper docstring
    1 point for setting an initial value
    1 point for the loop
    1 point for correctly updating the current maximum
    1 point for returning the proper maximum
    def find_max(iterable):
        """Return the maximum value of the iterable."""
        max_ = iterable[0]
        for item in iterable[1:]:
            if max_ < item:
                max_ = item
        return max_
  4. Rewrite the following code to use tuples instead of lists. Perform the corresponding operations or workarounds for these operations with a tuple that are demonstrated with a list in the snippet below. (4 points)
    l = [1, 2, 3, 4]
    l.append(l[-1] + l[-2])
    l.pop(0)  # Remove and return item at index (default last).
    print(l.count(3))  # Print number of occurrences of value.
    1 point for initializing the tuple
    1 point for creating a new tuple with the correct new element at end
    1 point for creating a new tuple without the first element
    1 point for printing the count(3) of the tuple
    t = (1, 2, 3, 4)
    t = t + (t[-1] + t[-2],)
    t = t[1:]
    print(t.count(3))
  5. Telephone numbers with letters
    In some countries it is a common practice to encode selected telephone numbers as text because they are easier to memorize that way. The letters of the text signal which button to press on a telephone keypad. See the image below as a reference:
    Source: Marnanel (via Wikimedia Commons)
    Implement a function as_numeric(text) that returns a string containing only the numbers that correspond to the input text.
    For example: as_numeric('0800 reimann') should return '0800 7346266'.
    Add a docstring with a doctest to receive full points. (6 points)
    1 point for the docstring
    1 point for the doctest
    3 points for the correct algorithm to produce the number string
    1 point for returning the string with the telephone number
    def as_numeric(number_name):
        """
        Take telephone number as name string and return string of digits.
        >>> as_numeric('0800 reimann')
        '0800 7346266'
        """
        number = ""
        number_name = number_name.lower()
        for char in number_name:
            if char in string.digits + ' ':
                number += char
            elif char in {'a', 'b', 'c'}:
                number += '2'
            elif char in {'d', 'e', 'f'}:
                number += '3'
            elif char in {'g', 'h', 'i'}:
                number += '4'
            elif char in {'j', 'k', 'l'}:
                number += '5'
            elif char in {'m', 'n', 'o'}:
                number += '6'
            elif char in {'p', 'q', 'r', 's'}:
                number += '7'
            elif char in {'t', 'u', 'v'}:
                number += '8'
            elif char in {'w', 'x', 'y', 'z'}:
                number += '9'
        return number