1AKIFT POS Test (Group A)

2023-01-31

Max. 15 points

Name:

Task Max. Achieved
1 6
2 7
3 2
Sum 15
Grading: >=14: 1, >=12: 2, >=10: 3, >=8: 4, <=7: 5

This test may done on a computer without an internet connection. Create a file named 2023-01-31_firstname_lastname.py. All of your code must be added to this file.
You may create additional functions and add code to test your solutions. You may also add data files to test your solution.

Once completed, contact your lecturer who will collect your solution.

  1. Write a function product(iterable) -> int in Python that takes a sequences of integers and returns the product of the numbers.
    [e.g. product((9, 2, 3)) ⇒ 9 * 2 * 3 = 54; product([3, 5, 4, 2]) ⇒ 120]. Add a proper docstring and a doctest to receive full points.
    (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 product
    1 point for returning the product
    def product(iterable) -> int:
        """
        Return the products of the numbers in iterable.
    
        >>> product((9, 2, 3))
        54
        """
        result = 1
        for e in iterable:
            result *= e
        return result
  2. 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.
    (7 points)
    1 point for a proper docstring
    1 point 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
  3. Add a shebang for executing the file with python. Use /usr/bin/env python as the default python interpreter. When your script is executed, print the result of product((9, 2, 3)) to the terminal. If the script is imported, nothing should be printed.
    (2 points)
    1 point for the proper shebang
    1 point for the ifmain pattern
    #!/usr/bin/env python
    
    #...
    
    
    if __name__ == "__main__":
        print(product((9, 2, 3)))