Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 205cb87

Browse files
committedJun 12, 2024·
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent b79e0c1 commit 205cb87

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed
 

‎maths/probability_of_n_heads_in_m_tossing.py

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,56 @@
1-
2-
31
import numpy as np
42

53

64
def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int:
75
"""
8-
The probability of getting exactly n heads in m tossing.
6+
The probability of getting exactly n heads in m tossing.
97
10-
Algorithm Explanation:
11-
If you toss 0 time -> 0 head
12-
Distribution [1] -> Meaning: 1 in the 0-index
8+
Algorithm Explanation:
9+
If you toss 0 time -> 0 head
10+
Distribution [1] -> Meaning: 1 in the 0-index
1311
14-
If you toss 1 time -> 0 or 1 head
15-
Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes
12+
If you toss 1 time -> 0 or 1 head
13+
Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes
1614
17-
If you toss 2 times -> 0 to 2 heads
18-
Distribution [0.25 0.5 0.25] ->
19-
Meaning: probability of n heads from the distribution
20-
{HH, HT, TH, TT}
15+
If you toss 2 times -> 0 to 2 heads
16+
Distribution [0.25 0.5 0.25] ->
17+
Meaning: probability of n heads from the distribution
18+
{HH, HT, TH, TT}
2119
22-
If you toss 3 times -> 0 to 3 heads
23-
Distribution [0.125 0.375 0.375 0.125] ->
24-
Meaning: probability of n heads from the distribution
25-
{HHH, HHT, HTH, HTT, THH, THT, TTH, TTT}
20+
If you toss 3 times -> 0 to 3 heads
21+
Distribution [0.125 0.375 0.375 0.125] ->
22+
Meaning: probability of n heads from the distribution
23+
{HHH, HHT, HTH, HTT, THH, THT, TTH, TTT}
2624
27-
Therefore,
28-
Probability_distribution(N+1) =
29-
[Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2
25+
Therefore,
26+
Probability_distribution(N+1) =
27+
[Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2
3028
31-
I used that method in my paper
32-
Titled: Uncertainty-aware Decisions in Cloud Computing:
33-
Foundations and Future Directions
34-
Journal: ACM Computing Surveys (CSUR)
29+
I used that method in my paper
30+
Titled: Uncertainty-aware Decisions in Cloud Computing:
31+
Foundations and Future Directions
32+
Journal: ACM Computing Surveys (CSUR)
3533
36-
>>> import numpy as np
37-
>>> Probability_of_n_heads_in_m_tossing(.2,.5)
38-
Traceback (most recent call last):
39-
...
40-
ValueError: The function only accepts integer values
41-
>>> Probability_of_n_heads_in_m_tossing(-1,5)
42-
Traceback (most recent call last):
43-
...
44-
ValueError: The function is not defined for negative values
45-
>>> Probability_of_n_heads_in_m_tossing(3,2)
46-
Traceback (most recent call last):
47-
...
48-
ValueError: Head count should be smaller than toss count
34+
>>> import numpy as np
35+
>>> Probability_of_n_heads_in_m_tossing(.2,.5)
36+
Traceback (most recent call last):
37+
...
38+
ValueError: The function only accepts integer values
39+
>>> Probability_of_n_heads_in_m_tossing(-1,5)
40+
Traceback (most recent call last):
41+
...
42+
ValueError: The function is not defined for negative values
43+
>>> Probability_of_n_heads_in_m_tossing(3,2)
44+
Traceback (most recent call last):
45+
...
46+
ValueError: Head count should be smaller than toss count
4947
50-
>>> Probability_of_n_heads_in_m_tossing(1,1)
51-
0.5
52-
>>> Probability_of_n_heads_in_m_tossing(0,2)
53-
0.25
54-
>>> Probability_of_n_heads_in_m_tossing(2,3)
55-
0.375
48+
>>> Probability_of_n_heads_in_m_tossing(1,1)
49+
0.5
50+
>>> Probability_of_n_heads_in_m_tossing(0,2)
51+
0.25
52+
>>> Probability_of_n_heads_in_m_tossing(2,3)
53+
0.375
5654
"""
5755
if head_count != int(head_count) or toss_count != int(toss_count):
5856
raise ValueError("The function only accepts integer values")

0 commit comments

Comments
 (0)
Please sign in to comment.