Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
34
Task/Knuths-algorithm-S/Python/knuths-algorithm-s-1.py
Normal file
34
Task/Knuths-algorithm-S/Python/knuths-algorithm-s-1.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from random import randrange
|
||||
|
||||
def s_of_n_creator(n):
|
||||
sample, i = [], 0
|
||||
def s_of_n(item):
|
||||
nonlocal i
|
||||
|
||||
i += 1
|
||||
if i <= n:
|
||||
# Keep first n items
|
||||
sample.append(item)
|
||||
elif randrange(i) < n:
|
||||
# Keep item
|
||||
sample[randrange(n)] = item
|
||||
return sample
|
||||
return s_of_n
|
||||
|
||||
if __name__ == '__main__':
|
||||
bin = [0]* 10
|
||||
items = range(10)
|
||||
print("Single run samples for n = 3:")
|
||||
s_of_n = s_of_n_creator(3)
|
||||
for item in items:
|
||||
sample = s_of_n(item)
|
||||
print(" Item: %i -> sample: %s" % (item, sample))
|
||||
#
|
||||
for trial in range(100000):
|
||||
s_of_n = s_of_n_creator(3)
|
||||
for item in items:
|
||||
sample = s_of_n(item)
|
||||
for s in sample:
|
||||
bin[s] += 1
|
||||
print("\nTest item frequencies for 100000 runs:\n ",
|
||||
'\n '.join("%i:%i" % x for x in enumerate(bin)))
|
||||
16
Task/Knuths-algorithm-S/Python/knuths-algorithm-s-2.py
Normal file
16
Task/Knuths-algorithm-S/Python/knuths-algorithm-s-2.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
class S_of_n_creator():
|
||||
def __init__(self, n):
|
||||
self.n = n
|
||||
self.i = 0
|
||||
self.sample = []
|
||||
|
||||
def __call__(self, item):
|
||||
self.i += 1
|
||||
n, i, sample = self.n, self.i, self.sample
|
||||
if i <= n:
|
||||
# Keep first n items
|
||||
sample.append(item)
|
||||
elif randrange(i) < n:
|
||||
# Keep item
|
||||
sample[randrange(n)] = item
|
||||
return sample
|
||||
1
Task/Knuths-algorithm-S/Python/knuths-algorithm-s-3.py
Normal file
1
Task/Knuths-algorithm-S/Python/knuths-algorithm-s-3.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
s_of_n = S_of_n_creator(3)
|
||||
Loading…
Add table
Add a link
Reference in a new issue