|
1 |
| - |
2 | 1 | import numpy as np
|
3 | 2 |
|
4 | 3 |
|
5 | 4 | def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int:
|
6 | 5 | """
|
7 |
| - The probability of getting exactly n heads in m tossing. |
| 6 | + The probability of getting exactly n heads in m tossing. |
8 | 7 |
|
9 |
| - Algorithm Explanation: |
10 |
| - If you toss 0 time -> 0 head |
11 |
| - 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 |
12 | 11 |
|
13 |
| - If you toss 1 time -> 0 or 1 head |
14 |
| - 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 |
15 | 14 |
|
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} |
| 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} |
20 | 19 |
|
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} |
| 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} |
25 | 24 |
|
26 |
| - Therefore, |
27 |
| - Probability_distribution(N+1) = |
28 |
| - [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 |
29 | 28 |
|
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) |
| 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) |
34 | 33 |
|
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 |
| 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 |
48 | 47 |
|
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 |
| 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 |
55 | 54 | """
|
56 | 55 | if head_count != int(head_count) or toss_count != int(toss_count):
|
57 | 56 | raise ValueError("The function only accepts integer values")
|
|
0 commit comments