From 2fe278f91c07a4fa9a3c71d2cfb2035ba18c536f Mon Sep 17 00:00:00 2001 From: Inex Code Date: Sun, 9 Oct 2022 21:58:14 +0300 Subject: [PATCH] Task 1 --- .vscode/settings.json | 6 ++++++ shell.nix | 25 +++++++++++++++++++++++++ task1/__init__.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 shell.nix create mode 100644 task1/__init__.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e5cf285 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "python.analysis.typeCheckingMode": "strict", + "python.formatting.provider": "black", + "python.linting.enabled": true, + "python.linting.pylintEnabled": true +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..a28a47a --- /dev/null +++ b/shell.nix @@ -0,0 +1,25 @@ +{ pkgs ? import { } }: +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 + ''; +} diff --git a/task1/__init__.py b/task1/__init__.py new file mode 100644 index 0000000..9f45ab2 --- /dev/null +++ b/task1/__init__.py @@ -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()