forked from ruppysuppy/Daily-Coding-Problem-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path071.py
More file actions
38 lines (27 loc) · 651 Bytes
/
071.py
File metadata and controls
38 lines (27 loc) · 651 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Problem:
Using a function rand7() that returns an integer from 1 to 7 (inclusive) with uniform
probability, implement a function rand5() that returns an integer from 1 to 5
(inclusive).
"""
from random import randint
import matplotlib.pyplot as plt
# rand7 implementation
def rand7() -> int:
return randint(1, 7)
def rand5() -> int:
val = rand7()
if val <= 5:
return val
return rand5()
if __name__ == "__main__":
values = []
for i in range(100_000):
values.append(rand5())
plt.hist(values, bins=5, edgecolor="black")
plt.show()
"""
SPECS:
TIME COMPLEXITY: O(1)
SPACE COMPLEXITY: O(1)
"""