1AKIFT POS Test (Group A)

2024-11-25

Max. 100 points

Name:

Task Max. Achieved
1 20
2 30
3 25
4 25
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 lists are mutable.
    The sole purpose of indentation in Python is to make the code easier to read.
    The "ifmain" pattern allows a module to behave differently when it is executed vs. imported.
    Python code can be edited with any regular text editor. No special editor is required.
    Python strings are mutable.
    while loops are used for repeated execution as long as an expression is true.
    Python modules provide namespaces.
    sys.path is used to find Python modules when importing them.
  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'))
      None
    2. def sum(a, b):
          return a + b + c
      sum(4, 2, 3)
      ERROR
    3. def area(length, width):
          return length * width
      print(area(5, 2))
      10
    4. l = [4, 3, 2]
      print(l.sort())
      None
    5. import math
      print(circumference(0.5 / math.pi))
      ERROR
    6. print({1, 2, 10, 2, 1, 5})
      {1, 2, 10, 5}
  3. Write a function filter_by_divisor(l, divisor) in Python that takes a list of integers and an integer divisor. The function must return a list containing only the elements of the original list that can be divided by divisor without a remainder. For example
    filter_by_divisor([], 3)  # -> []
    filter_by_divisor([9], 2)  # -> []
    filter_by_divisor([9], 3)  # -> [9]
    filter_by_divisor([9, 2, 3], 3)  # -> [9, 3]
    filter_by_divisor([3, 5, -4, 2], 2)  # -> [-4, 2]
    filter_by_divisor([3, 5, -4, 2], 5)  # -> [5]
    (25 points)
    5 points for initializing the resulting (empty) list
    5 points for a correct loop
    5 points for a condition ensuring the remainder to be 0
    5 points for appending only the correct elements to the result
    5 points for returning the list with the correct elements
    def filter_by_divisor(l: List[int], divisor: int) -> List[int]:
        """Return a list containing elements of `l` divisible by `divisor`."""
        result = []
        for e in l:
            if e % divisor == 0:
                result.append(e)
        return result
  4. Write a function cumsum(l) in Python that takes a list of integers and returns a list containing the cumulative sum of the numbers. For example
    cumsum([])  # -> []
    cumsum([9])  # -> [9]
    cumsum([9, 2, 3])  # -> [9, 9+2, 9+2+3] -> [9, 11, 14]
    cumsum([3, 5, -4, 2])  # -> [3, 3+5, 3+5+(-4), 3+5+(-4)+2] -> [3, 8, 4, 6]
    Don't forget to deal with empty lists. (25 points)
    5 points for returning correct result for empty lists
    5 points for correctly dealing with the first element
    5 points for correct loop
    5 points for correctly adding all subsequent elements
    5 points for returning the correct result
    def cumsum(l: List[int]) -> List[int]:
        """Return the cumulative sum of the given list."""
        if not l: return []
        result = l[:1]
        for e in l[1:]:
            result.append(result[-1] + e)
        return result