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