Skip to content

Commit cc54095

Browse files
committed
more descriptibe arguments II
1 parent 5d460aa commit cc54095

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

data_structures/hashing/bloom_filter.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,28 @@ def random_string(size: int) -> str:
7070
return "".join(choices(ascii_lowercase + " ", k=size))
7171

7272

73-
def test_probability(bits: int = 64, n: int = 20) -> None:
74-
b = Bloom(size=bits)
73+
def test_probability(filter_bits: int = 64, added_elements: int = 20) -> None:
74+
b = Bloom(size=filter_bits)
7575

7676
k = len(b.HASH_FUNCTIONS)
77-
estimated_error_rate_beforehand = (1 - (1 - 1 / bits) ** (k * n)) ** k
77+
estimated_error_rate_beforehand = (
78+
1 - (1 - 1 / filter_bits) ** (k * added_elements)
79+
) ** k
7880

79-
added = {random_string(10) for i in range(n)}
80-
for a in added:
81-
b.add(a)
81+
not_added = {random_string(10) for i in range(1000)}
82+
for _ in range(added_elements):
83+
b.add(not_added.pop())
8284

8385
n_ones = bin(b.bitstring).count("1")
84-
estimated_error_rate = (n_ones / bits) ** k
86+
estimated_error_rate = (n_ones / filter_bits) ** k
8587

86-
not_added = {random_string(10) for i in range(1000)}
8788
errors = 0
8889
for string in not_added:
8990
if b.exists(string):
9091
errors += 1
9192
error_rate = errors / len(not_added)
9293

93-
print(f"total = {len(not_added)}, errors = {errors}, error_rate = {error_rate}")
94+
print(f"error_rate = {errors}/{len(not_added)} = {error_rate}")
9495
print(f"{estimated_error_rate=}")
9596
print(f"{estimated_error_rate_beforehand=}")
9697

0 commit comments

Comments
 (0)