1AKIFT POS Test (Group A)

2022-12-07

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, any block of code creates scope.
    In Python it is recommended to use two spaces for indentation.
    The "ifmain" pattern allows a module to behave differently when it is executed vs. imported.
    Python offers a "do-while" loop.
    Python code written with micro can be edited with another editor afterwards.
    A Python sequence type can be indexed, sliced and iterated.
    Strings in Python are immutable.
    Python lists are mutable.
    Python tuples are mutable.
    Python dictionaries are mutable.
    Doctests are a perfect way of testing your code.
    Comments are more powerful than 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 average(a, b, c):
          return (a + b + c) / 3.0
      average(4, 2, 3)
      -
    2. def area(length, width):
          return length * width
      print(area(5, 2))
      10
    3. s = 'Hello World!'
      s.upper()
      print(s)
      Hello World!
    4. l = [4, 2, 3, 1]
      print(l.sort())
      None
    5. def fun(a, b, c=5):
          return a + b + c
      print(fun(10))
      Error
  3. Finish the program below. The given search term needle should be searched in all of the files in the list of relative filenames. The program should count the non-overlapping occurrences of needle in all the given files and print it to standard output. The comparison must not be case sensitive. Use the string class' S.count(sub[, start[, end]]) -> int method which returns the number of non-overlapping occurrences of substring sub in string S[start:end]. (6 points)
    #!/usr/bin/env python
    filenames = input('Files: ')
    needle = input('Needle: ').lower()
    matches = 0
    filenames = filenames.split(',')  # create list of filenames
    1 point for looping over the filenames
    1 point for opening the file correctly
    1 point for reading the content
    1 point for converting the content to lowercase
    1 point for correctly using the count function
    1 point for printing the correct number of matches
    for filename in filenames:
        with open(filename) as f:
            content = f.read().lower()
            matches += content.count(needle)
    print(matches)
  4. Rewrite the following for loop as while loop. (4 points)
    for i in range(0, 10, 3):
        print(f'{i} * {i} = {i * i}')
    1 point for initializing the counter variable
    1 point for the while loop condition
    1 point for the correct print statement
    1 point for correctly incrementing the loop counter
    i = 0
    while i < 10:
        print(f'{i} * {i} = {i * i}')
        i += 3
  5. Write a function sumproduct(first iterable, second iterable) in Python that takes two sequences of numbers with equal length and returns the sum of the product of the numbers.
    [e.g. sumproduct((1, 3, 4), (9, 2, 3)) ⇒ 1*9 + 3*2 + 4*3 = 27; sumproduct([3, 5], [2, 4]) ⇒ 26]. Add a proper docstring and a doctest to receive full points.
    Grading: 1 point for correct function signature, 1 point for proper docstring, 1 point for a doctest, 2 for returning the correct result. (6 points)
    1 point for the function signature
    1 point for the docstring
    1 point for the doctest
    2 points for the correct calculation of the sumproduct
    1 point for returning the sum
    def sumproduct(first, second):
        """
        Return the sum of the products of `zip(first, second)`.
    
        >>> sumproduct((1, 3, 4), (9, 2, 3))
        27
        """
        sum = 0
        for i, j in zip(first, second):
            sum += i * j
        return sum