2023-01-31
Max. 15 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 6 | |
2 | 7 | |
3 | 2 | |
Sum | 15 |
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.
product(iterable) -> int
in Python that takes a sequences of integers and returns
the product of the numbers.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.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
2022-02-02T19:49,1175 2022-02-02T19:59,1235 2022-02-02T20:09,1481 ... 2022-02-02T22:09,753 2022-02-02T22:19,819Implement a function
average_co2(filename: str) -> float
that returns
the average CO2 in the given file.
Add a proper docstring to receive full points.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
/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.#!/usr/bin/env python
#...
if __name__ == "__main__":
print(product((9, 2, 3)))