From 873e6f528752861fec12a36f450558fe6aead59f Mon Sep 17 00:00:00 2001 From: Oleksandr Barabanov Date: Sun, 22 Dec 2024 19:55:04 +0200 Subject: [PATCH 1/5] Solution --- .flake8 | 2 +- app/main.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) 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..07373215 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,32 @@ -# write your code here +import random +import matplotlib.pyplot as plt + + +def flip_coin(): + results = {i: 0 for i in range(11)} + num_trials = 10000 + + for _ in range(num_trials): + count = sum(random.choice([0, 1]) for _ in range(10)) + results[count] += 1 + + results = {key: (value // num_trials) * 100 for key, value in results.items()} + return results + + +def draw_gaussian_distribution_graph(distribution): + keys = list(distribution.keys()) + values = list(distribution.values()) + + plt.bar(keys, values, color='skyblue', edgecolor='black') + plt.title("Gaussian Distribution of Coin Flips") + plt.xlabel("Number of Heads") + plt.ylabel("Percentage") + plt.xticks(range(11)) # Set x-axis ticks from 0 to 10 + plt.grid(axis='y', linestyle='--', alpha=0.7) + plt.show() + + coin_flip_distribution = flip_coin() + print(coin_flip_distribution) + + draw_gaussian_distribution_graph(coin_flip_distribution) From 79089819c7ac369fb85dec1e55d59e2659a0f350 Mon Sep 17 00:00:00 2001 From: Oleksandr Barabanov Date: Sun, 22 Dec 2024 20:08:14 +0200 Subject: [PATCH 2/5] Solution --- app/main.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index 07373215..2dff4a30 100644 --- a/app/main.py +++ b/app/main.py @@ -2,7 +2,7 @@ import matplotlib.pyplot as plt -def flip_coin(): +def flip_coin() -> dict: results = {i: 0 for i in range(11)} num_trials = 10000 @@ -10,20 +10,21 @@ def flip_coin(): count = sum(random.choice([0, 1]) for _ in range(10)) results[count] += 1 - results = {key: (value // num_trials) * 100 for key, value in results.items()} + for key, value in results.items(): + results[key]: (value // num_trials) * 100 return results -def draw_gaussian_distribution_graph(distribution): +def draw_gaussian_distribution_graph(distribution: dict) -> None: keys = list(distribution.keys()) values = list(distribution.values()) - plt.bar(keys, values, color='skyblue', edgecolor='black') + plt.bar(keys, values, color="skyblue", edgecolor="black") plt.title("Gaussian Distribution of Coin Flips") plt.xlabel("Number of Heads") plt.ylabel("Percentage") plt.xticks(range(11)) # Set x-axis ticks from 0 to 10 - plt.grid(axis='y', linestyle='--', alpha=0.7) + plt.grid(axis="y", linestyle="--", alpha=0.7) plt.show() coin_flip_distribution = flip_coin() From cd38c34d1e7edaa3c8af440efc76b391ec98fa6c Mon Sep 17 00:00:00 2001 From: Oleksandr Barabanov Date: Sun, 22 Dec 2024 20:08:14 +0200 Subject: [PATCH 3/5] Solution --- app/main.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index 07373215..2dff4a30 100644 --- a/app/main.py +++ b/app/main.py @@ -2,7 +2,7 @@ import matplotlib.pyplot as plt -def flip_coin(): +def flip_coin() -> dict: results = {i: 0 for i in range(11)} num_trials = 10000 @@ -10,20 +10,21 @@ def flip_coin(): count = sum(random.choice([0, 1]) for _ in range(10)) results[count] += 1 - results = {key: (value // num_trials) * 100 for key, value in results.items()} + for key, value in results.items(): + results[key]: (value // num_trials) * 100 return results -def draw_gaussian_distribution_graph(distribution): +def draw_gaussian_distribution_graph(distribution: dict) -> None: keys = list(distribution.keys()) values = list(distribution.values()) - plt.bar(keys, values, color='skyblue', edgecolor='black') + plt.bar(keys, values, color="skyblue", edgecolor="black") plt.title("Gaussian Distribution of Coin Flips") plt.xlabel("Number of Heads") plt.ylabel("Percentage") plt.xticks(range(11)) # Set x-axis ticks from 0 to 10 - plt.grid(axis='y', linestyle='--', alpha=0.7) + plt.grid(axis="y", linestyle="--", alpha=0.7) plt.show() coin_flip_distribution = flip_coin() From ddd0bfa17e258b967bcc910ba3be488cdb893ecd Mon Sep 17 00:00:00 2001 From: Oleksandr Barabanov Date: Tue, 24 Dec 2024 12:32:51 +0200 Subject: [PATCH 4/5] Solution --- app/main.py | 42 +++++++++++++++++++++++++----------------- app/requirements.txt | 5 +++++ 2 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 app/requirements.txt diff --git a/app/main.py b/app/main.py index 2dff4a30..3ec034c5 100644 --- a/app/main.py +++ b/app/main.py @@ -1,33 +1,41 @@ -import random +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() -> dict: + +def flip_coin(num_trials: int = 10000) -> dict[int, float]: results = {i: 0 for i in range(11)} - num_trials = 10000 for _ in range(num_trials): - count = sum(random.choice([0, 1]) for _ in range(10)) - results[count] += 1 + heads_count = simulate_ten_flips() + results[heads_count] += 1 + + for heads in results: + results[heads] = round((results[heads] / num_trials) * 100, 2) - for key, value in results.items(): - results[key]: (value // num_trials) * 100 return results -def draw_gaussian_distribution_graph(distribution: dict) -> None: - keys = list(distribution.keys()) - values = list(distribution.values()) +def draw_gaussian_distribution_graph(data: dict[int, float]) -> None: + x_coords = list(data.keys()) + y_coords = list(data.values()) - plt.bar(keys, values, color="skyblue", edgecolor="black") - plt.title("Gaussian Distribution of Coin Flips") + plt.bar(x_coords, y_coords, color="blue", alpha=0.7, edgecolor="black") plt.xlabel("Number of Heads") plt.ylabel("Percentage") - plt.xticks(range(11)) # Set x-axis ticks from 0 to 10 + plt.title("Distribution of Heads in 10 Coin Flips") + plt.xticks(range(11)) plt.grid(axis="y", linestyle="--", alpha=0.7) - plt.show() + plt.savefig("distribution_graph.png") - coin_flip_distribution = flip_coin() - print(coin_flip_distribution) - draw_gaussian_distribution_graph(coin_flip_distribution) +if __name__ == "__main__": + results = flip_coin() + print(results) + draw_gaussian_distribution_graph(results) \ No newline at end of file diff --git a/app/requirements.txt b/app/requirements.txt new file mode 100644 index 00000000..5f1ea6de --- /dev/null +++ b/app/requirements.txt @@ -0,0 +1,5 @@ +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 From a20b24a19e196a6296fa437bc1a8f9ba5e48557b Mon Sep 17 00:00:00 2001 From: Oleksandr Barabanov Date: Tue, 24 Dec 2024 12:40:20 +0200 Subject: [PATCH 5/5] Solution --- app/main.py | 2 +- app/requirements.txt | 5 ----- requirements.txt | 2 ++ 3 files changed, 3 insertions(+), 6 deletions(-) delete mode 100644 app/requirements.txt diff --git a/app/main.py b/app/main.py index 3ec034c5..e82864f8 100644 --- a/app/main.py +++ b/app/main.py @@ -38,4 +38,4 @@ def draw_gaussian_distribution_graph(data: dict[int, float]) -> None: if __name__ == "__main__": results = flip_coin() print(results) - draw_gaussian_distribution_graph(results) \ No newline at end of file + draw_gaussian_distribution_graph(results) diff --git a/app/requirements.txt b/app/requirements.txt deleted file mode 100644 index 5f1ea6de..00000000 --- a/app/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -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 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