1AKIFT POS Test (Group A)

2021-02-03

Max. 32 points

Name:

Task Max. Achieved
1 6
2 10
3 8
4 8
Sum 32
Grading: <16: 5, 16-20: 4, 21-24: 3, 25-28: 2, 29-32: 1
    1. Python
      Answer the following statements indicating whether they are True or False.
      0-4 correct: 0 points, 5 correct: 1 point, 6 correct: 2 points, 7 correct: 3 points, 8 correct: 4 points.
      Statement True False
      In Python, any block of code creates scope.
      Python has different versions. Old code may not work with newer versions, new code may not work with older versions.
      Execution of all Python programs starts in main().
      pip allows installing and updating third party libraries.
      Python code written with VSCodium cannot be edited with another editor afterwards.
      A Python sequence type can be indexed, sliced and iterated.
      Strings in Python are immutable.
      Python lists are immutable.
    2. C
      Answer the following statements indicating whether they are True or False.
      0-2 correct: 0 points, 3 correct: 1 point, 4 correct: 2 points.
      Statement True False
      In C, any block of code creates scope.
      The entry point of any C program is main().
      Clang and gcc are aliases for the same C compiler.
      C has different versions and different compilers. A program working with one compiler may not work with another compiler.
  1. 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 square(length):
          return length * length
      print(square(5))
      25
    3. s = 'Good luck!'
      s.upper()
      print(s)
      Good luck!
    4. sum = lambda x, y: x + y
      print (sum (3, 4))
      7
    5. from collections import namedtuple
      Point = namedtuple ('Point', ('x', 'y'))
      A = Point(x=2, y=3)
      -
  2. Implement rot13(text) assuming that text only contains upper case characters. Use the ord(char)->int and chr(i:int)->str functions in your implementation. Add a proper docstring in order to receive full points.
    1 point for correct function signature
    1 point for proper docstring
    1 point for result list
    1 point for for loop
    2 points for correct conditions
    1 point for joining the list
    1 point for returning the correct result
    def rot13(text):
        """Return the input string after returning the characters by 13 places."""
        result = []
        for char in text:
            if 'A' <= char <= 'M':
                num = ord(char) + 13
            elif 'N' <= char <= 'Z':
                num = ord(char) - 13
            else:
                num = ord(char)
            result.append(chr(num))
        return "".join(result)
  3. You're in charge of analyzing CO2 data measured by multiple sensors in your company's building. You will get the sensor data weekly, one file per sensor. The first analysis you're planning to do is to calculate the average CO2 pollution per week for each sensor. The data in each file is straightforward. One measurement per line, starting with a timestamp, then a comma ",", and then the CO2 value in PPM. An input file looks like
    2022-02-02T19:49,1175
    2022-02-02T19:59,1235
    2022-02-02T20:09,1481
    ...
    2022-02-02T22:09,753
    2022-02-02T22:19,819
    
    Implement a function average_co2(filename: str) -> float that returns the average CO2 in the given file. Add a proper docstring to receive full points.
    1 point for correct function signature
    1 point for a proper docstring
    1 points for reading the file
    1 point for correctly splitting the input lines
    1 point for converting the PPM to int
    1 point for correct counter variable
    1 point for correctly summing up the values
    1 for returning the correct result
    def average_co2(filename: str) -> float:
        """
        Return average CO2 from the given file.
        """
        count = 0
        total = 0
        with open(filename) as f:
            for line in f:
                total += int(line.split(',')[-1])
                count += 1
        return total / count