1AKIFT POS Test (Group A)

2024-11-27

Max. 100 points

Name:

Task Max. Achieved
1 20
2 30
3 20
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-4 correct: 0 points, 5 correct: 5 points, 6 correct: 10 points, 7 correct: 15 points, 8 correct: 20 points.
    Statement True False
    Python dictionaries are great for intersection and union of two collections.
    Python tuples are mutable.
    sys.exit(0) indicates success to the operating system.
    a, b = b, a is a good way for swapping values in Python
    Python strings are immutable.
    for loops are used for repeated execution as long as an expression is true.
    Python blocks create a new scope.
    Python supports default parameters and keyword arguments.
  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 "-".
    5 points for each correct answer.
    1. l = ('a', 'b', 'c', )
      print(l.append('d'))
      ERROR
    2. def sum(a, b):
          return a + c
      sum(4, 2)
      ERROR
    3. def concat(c1, c2):
          return c1 + c2
      print(concat([1, 2], [3]))
      [1, 2, 3]
    4. s = 'c++'
      print(s.upper())
      C++
    5. import math
      round(math.sin(0.0))
      -
    6. print([1, 2].append(3))
      None
  3. 20 points
    Rewrite the following for loop as while loop.
    for i in range(10, 0, -2):
        print(f'{i*i=}')
    5 points for initializing i
    5 points for the correct loop condition
    5 points for copy and paste of the print statement
    5 points for decrementing i by 2
    i = 10
    while i > 0:
        print(f'{i*i=}')
        i -= 2
  4. 30 points
    Write a function count_words(text, words) in Python that takes a text and a sequence of words and returns a dictionary containing each of the words occurs in the text. Add a docstring to receive all points. Some examples of possible inputs and outputs:
    count_words('', ())  # -> {}
    count_words('', ('foo', 'bar'))  # -> {'foo': 0, 'bar': 0}
    count_words('foo is bar', ('foo', 'bar'))  # -> {'foo': 1, 'bar': 1}
    count_words('foo is bar', ('is', ))  # -> {'is': 1}
    count_words('fi foo bar boo foo', ('foo', 'bar'))  # -> {'foo': 2, 'bar': 1}
    5 points for a proper docstring
    5 points for the initial dictionary with 0 values
    5 points for splitting the input text into words
    5 points for correct loop
    5 points for testing if each word is in words
    5 points for correctly incrementing the values and returning the dict
    from collections.abc import Sequence  # solely for type hints
    def count_words(text: str, words: Sequence[str]) -> dict[str, int]:
        """Return how many times each word in words is in text."""
        words = set(words)
        occurrences = dict.fromkeys(words, 0)
        for word in text.split():
            if word in words:
                occurrences[word] += 1
        return occurrences