From 3ada27e32be2656db69abe83401ac3b607fcf3ec Mon Sep 17 00:00:00 2001 From: Grigoriy Sventsitskiy Date: Sat, 21 Dec 2024 23:59:13 +0200 Subject: [PATCH 1/5] Solution --- app/main.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index fa56336e..c4515a04 100644 --- a/app/main.py +++ b/app/main.py @@ -1 +1,25 @@ -# write your code here +import random +from pprint import pprint + + +def flip_coin() -> dict[int: float]: + heads_all_counts = [] + for _ in range(10000): + heads_count = 0 + for _ in range(10): + if random.choice(['heads', 'tails']) == 'heads': + heads_count += 1 + heads_all_counts.append(heads_count) + return {k: heads_all_counts.count(k) / len(heads_all_counts) for k in range(11)} + + + + +if __name__ == '__main__': + cache = set() + + for _ in range(20): + coins = flip_coin() + pprint(coins) + cache.add(tuple(result for result in coins.values())) + ## pprint(cache) \ No newline at end of file From 14460957228fddb46bb66c343d0aaae5a8eb9631 Mon Sep 17 00:00:00 2001 From: Grigoriy Sventsitskiy Date: Sun, 22 Dec 2024 01:14:59 +0200 Subject: [PATCH 2/5] Solution --- app/main.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index c4515a04..65fa0453 100644 --- a/app/main.py +++ b/app/main.py @@ -1,25 +1,44 @@ import random from pprint import pprint +import matplotlib.pyplot as plt +import numpy as np def flip_coin() -> dict[int: float]: heads_all_counts = [] for _ in range(10000): heads_count = 0 + random.seed(random.randint(1,9999999)) for _ in range(10): if random.choice(['heads', 'tails']) == 'heads': heads_count += 1 heads_all_counts.append(heads_count) return {k: heads_all_counts.count(k) / len(heads_all_counts) for k in range(11)} +def draw_gaussian_distribution_graph(): + coins = flip_coin() + xpoints = np.array(list(coins.keys())) + ypoints = np.array([round(x * 100, 2) for x in list(coins.values())]) + plt.xlabel('Heads count') + plt.ylabel('Drop percentage %') + plt.title('Gaussian distribution') + plt.ylim([0,100]) + plt.xlim([0,10]) + + plt.plot(xpoints, ypoints) + plt.xticks(range(11)) + plt.yticks(range(0,101,10)) + plt.show() + if __name__ == '__main__': cache = set() for _ in range(20): coins = flip_coin() - pprint(coins) + cache.add(tuple(result for result in coins.values())) - ## pprint(cache) \ No newline at end of file + print(cache) + draw_gaussian_distribution_graph() \ No newline at end of file From 098d45e590742624a77e06d822f0c7e28c5a96e5 Mon Sep 17 00:00:00 2001 From: Grigoriy Sventsitskiy Date: Sun, 22 Dec 2024 01:49:08 +0200 Subject: [PATCH 3/5] Solution (pytest and flake9 - pass) --- app/main.py | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/app/main.py b/app/main.py index 65fa0453..e8a16568 100644 --- a/app/main.py +++ b/app/main.py @@ -1,39 +1,36 @@ import random -from pprint import pprint import matplotlib.pyplot as plt import numpy as np def flip_coin() -> dict[int: float]: - heads_all_counts = [] + heads_all_counts = {k: 0 for k in range(0, 11)} for _ in range(10000): - heads_count = 0 - random.seed(random.randint(1,9999999)) - for _ in range(10): - if random.choice(['heads', 'tails']) == 'heads': - heads_count += 1 - heads_all_counts.append(heads_count) - return {k: heads_all_counts.count(k) / len(heads_all_counts) for k in range(11)} - -def draw_gaussian_distribution_graph(): + heads_count = [random.randint(0, 1) for _ in range(0, 10)].count(1) + heads_all_counts[heads_count] += 1 + for key, value in heads_all_counts.items(): + heads_all_counts[key] = value / 100 + return heads_all_counts + + +def draw_gaussian_distribution_graph() -> None: coins = flip_coin() xpoints = np.array(list(coins.keys())) - ypoints = np.array([round(x * 100, 2) for x in list(coins.values())]) - + ypoints = np.array([round(x, 2) for x in list(coins.values())]) - plt.xlabel('Heads count') - plt.ylabel('Drop percentage %') - plt.title('Gaussian distribution') - plt.ylim([0,100]) - plt.xlim([0,10]) + plt.xlabel("Heads count") + plt.ylabel("Drop percentage %") + plt.title("Gaussian distribution") + plt.ylim([0, 100]) + plt.xlim([0, 10]) plt.plot(xpoints, ypoints) plt.xticks(range(11)) - plt.yticks(range(0,101,10)) + plt.yticks(range(0, 101, 10)) plt.show() -if __name__ == '__main__': +if __name__ == "__main__": cache = set() for _ in range(20): @@ -41,4 +38,4 @@ def draw_gaussian_distribution_graph(): cache.add(tuple(result for result in coins.values())) print(cache) - draw_gaussian_distribution_graph() \ No newline at end of file + draw_gaussian_distribution_graph() From 67872218da2e04b263fc1f134751945f57b1aba4 Mon Sep 17 00:00:00 2001 From: Grigoriy Sventsitskiy Date: Sun, 22 Dec 2024 01:53:20 +0200 Subject: [PATCH 4/5] Solution (pytest and flake8 - pass) --- app/main.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/app/main.py b/app/main.py index e8a16568..5ff07453 100644 --- a/app/main.py +++ b/app/main.py @@ -28,14 +28,3 @@ def draw_gaussian_distribution_graph() -> None: plt.xticks(range(11)) plt.yticks(range(0, 101, 10)) plt.show() - - -if __name__ == "__main__": - cache = set() - - for _ in range(20): - coins = flip_coin() - - cache.add(tuple(result for result in coins.values())) - print(cache) - draw_gaussian_distribution_graph() From b4dd5c9486a4a2649d21882bf5bc00ad697e10d3 Mon Sep 17 00:00:00 2001 From: Grigoriy Sventsitskiy Date: Sun, 22 Dec 2024 01:58:08 +0200 Subject: [PATCH 5/5] Solution (pytest and flake8 - pass). Import matplotlib and numpy move to function --- app/main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 5ff07453..9d6cad29 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,4 @@ import random -import matplotlib.pyplot as plt -import numpy as np def flip_coin() -> dict[int: float]: @@ -14,6 +12,9 @@ def flip_coin() -> dict[int: float]: def draw_gaussian_distribution_graph() -> None: + import matplotlib.pyplot as plt + import numpy as np + coins = flip_coin() xpoints = np.array(list(coins.keys())) ypoints = np.array([round(x, 2) for x in list(coins.values())])