its-tasks/task1/__init__.py

42 lines
1.2 KiB
Python

"""
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()