From 4e8628b5d0d4e96573126685c1b5ae812a3e3833 Mon Sep 17 00:00:00 2001 From: Bohdan Khudiakov Date: Tue, 24 Dec 2024 07:30:00 +0200 Subject: [PATCH] Solution --- app/main.py | 42 +++++++++++++++++++++++++++++++++++++++++- requirements.txt | 2 ++ 2 files changed, 43 insertions(+), 1 deletion(-) 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..47836339 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 \ No newline at end of file