-
Notifications
You must be signed in to change notification settings - Fork 0
/
015.py
44 lines (33 loc) · 1.14 KB
/
015.py
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
39
40
41
42
43
44
"""
Problem:
Given a stream of elements too large to store in memory, pick a random element from the
stream with uniform probability.
"""
from random import randint
import matplotlib.pyplot as plt
from typing import Generator
def element_stream() -> Generator[int, None, None]:
# generator function to simulate a stream of elements too large to store in memory
while True:
yield randint(1, 10_000)
def random_selector(generator: Generator[int, None, None]) -> int:
# getting 10 elements from the stream of elements
arr = [next(generator) for i in range(10)]
# selecting a random element from the array of 10 elements
pos = randint(0, 9)
return arr[pos]
if __name__ == "__main__":
generator = element_stream()
# storing the selected elements for plotting a graph
values = []
for i in range(100_000):
values.append(random_selector(generator))
# plotting the histogram of frequencies of the selected elements (not stated in
# problem, added to display the uniform distribution)
plt.hist(values, edgecolor="black")
plt.show()
"""
SPECS:
TIME COMPLEXITY: O(1)
SPACE COMPLEXITY: O(1)
"""