Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
bodiakof committed Dec 24, 2024
1 parent 9ece647 commit 4e8628b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 41 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4e8628b

Please sign in to comment.