diff --git a/.flake8 b/.flake8 index d7459204..3f5e06ff 100644 --- a/.flake8 +++ b/.flake8 @@ -4,4 +4,4 @@ ignore = E203, E266, W503, ANN002, ANN003, ANN101, ANN102, ANN401, N807, N818 max-line-length = 79 max-complexity = 18 select = B,C,E,F,W,T4,B9,ANN,Q0,N8,VNE -exclude = venv, tests +exclude = venv, tests, .venv diff --git a/app/main.py b/app/main.py index fa56336e..e82864f8 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,41 @@ -# write your code here +from random import randint + +import matplotlib +import matplotlib.pyplot as plt +matplotlib.use("Agg") + + +def simulate_ten_flips() -> int: + return sum(randint(0, 1) for _ in range(10)) + + +def flip_coin(num_trials: int = 10000) -> dict[int, float]: + results = {i: 0 for i in range(11)} + + for _ in range(num_trials): + heads_count = simulate_ten_flips() + results[heads_count] += 1 + + for heads in results: + results[heads] = round((results[heads] / num_trials) * 100, 2) + + return results + + +def draw_gaussian_distribution_graph(data: dict[int, float]) -> None: + x_coords = list(data.keys()) + y_coords = list(data.values()) + + plt.bar(x_coords, y_coords, color="blue", alpha=0.7, edgecolor="black") + plt.xlabel("Number of Heads") + plt.ylabel("Percentage") + plt.title("Distribution of Heads in 10 Coin Flips") + plt.xticks(range(11)) + plt.grid(axis="y", linestyle="--", alpha=0.7) + plt.savefig("distribution_graph.png") + + +if __name__ == "__main__": + results = flip_coin() + print(results) + draw_gaussian_distribution_graph(results) diff --git a/requirements.txt b/requirements.txt index 3f202d6e..a8e35175 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ flake8-quotes==3.3.1 flake8-variables-names==0.0.5 pep8-naming==0.13.2 pytest==7.1.3 + +matplotlib~=3.10.0