Task 1
This commit is contained in:
commit
2fe278f91c
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"python.analysis.typeCheckingMode": "strict",
|
||||
"python.formatting.provider": "black",
|
||||
"python.linting.enabled": true,
|
||||
"python.linting.pylintEnabled": true
|
||||
}
|
25
shell.nix
Normal file
25
shell.nix
Normal file
|
@ -0,0 +1,25 @@
|
|||
{ pkgs ? import <nixpkgs> { } }:
|
||||
let
|
||||
its-python = pkgs.python310.withPackages (p: with p; [
|
||||
setuptools
|
||||
portalocker
|
||||
pytz
|
||||
pytest
|
||||
pytest-mock
|
||||
pytest-datadir
|
||||
coverage
|
||||
pylint
|
||||
pydantic
|
||||
black
|
||||
]);
|
||||
in
|
||||
pkgs.mkShell {
|
||||
buildInputs = [
|
||||
its-python
|
||||
pkgs.black
|
||||
];
|
||||
shellHook = ''
|
||||
PYTHONPATH=${its-python}/${its-python.sitePackages}
|
||||
# maybe set more env-vars
|
||||
'';
|
||||
}
|
41
task1/__init__.py
Normal file
41
task1/__init__.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
"""
|
||||
Task for the lecture 1.
|
||||
|
||||
1. Write a function to fill a list of 1000000 elements with random numbers from 0 to 999.
|
||||
2. Write a function to count histogram of the list with 10 bins.
|
||||
i.e. the function should return a list of 10 elements, where the first element
|
||||
is the number of elements in the list that are in the range [0, 99],
|
||||
the second element is the number of elements in the list that are in the range [100, 199],
|
||||
and so on.
|
||||
3. Determine the time of execution of the functions. Perfrorm the `calcHist` function 100 times and'
|
||||
determine the max, min and average time of execution.
|
||||
"""
|
||||
|
||||
import random
|
||||
import timeit
|
||||
|
||||
|
||||
def fill_list() -> list[int]:
|
||||
"""Fill a list with random numbers."""
|
||||
return [random.randint(0, 999) for _ in range(1000000)]
|
||||
|
||||
|
||||
def count_histogram(lst: list[int]) -> list[int]:
|
||||
"""Count histogram of the list."""
|
||||
hist = [0] * 10
|
||||
for number in lst:
|
||||
hist[number // 100] += 1
|
||||
return hist
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function."""
|
||||
lst = fill_list()
|
||||
times = [timeit.timeit(lambda: count_histogram(lst), number=1) for _ in range(100)]
|
||||
print(f"Max time: {max(times)}")
|
||||
print(f"Min time: {min(times)}")
|
||||
print(f"Average time: {sum(times) / len(times)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in a new issue