-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |