mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-28 08:33:36 +00:00
has functions constant
This commit is contained in:
parent
173ab0ea96
commit
08bc970b7a
@ -7,6 +7,9 @@ import string
|
|||||||
|
|
||||||
|
|
||||||
class Bloom:
|
class Bloom:
|
||||||
|
# number of hash functions is fixed
|
||||||
|
HASH_FUNCTIONS = (sha256, md5)
|
||||||
|
|
||||||
def __init__(self, size=8):
|
def __init__(self, size=8):
|
||||||
self.bitstring = 0b0
|
self.bitstring = 0b0
|
||||||
self.size = size
|
self.size = size
|
||||||
@ -42,7 +45,7 @@ class Bloom:
|
|||||||
|
|
||||||
def hash(self, value):
|
def hash(self, value):
|
||||||
res = 0b0
|
res = 0b0
|
||||||
for func in (sha256, md5):
|
for func in HASH_FUNCTIONS:
|
||||||
b = func(value.encode()).digest()
|
b = func(value.encode()).digest()
|
||||||
position = int.from_bytes(b, "little") % self.size
|
position = int.from_bytes(b, "little") % self.size
|
||||||
res |= 2**position
|
res |= 2**position
|
||||||
@ -74,8 +77,7 @@ def test_probability(m=64, n=20):
|
|||||||
for a in added:
|
for a in added:
|
||||||
b.add(a)
|
b.add(a)
|
||||||
|
|
||||||
# number of hash functions is fixed
|
k = len(b.HASH_FUNCIONS)
|
||||||
k = 2
|
|
||||||
|
|
||||||
n_ones = bin(b.bitstring).count("1")
|
n_ones = bin(b.bitstring).count("1")
|
||||||
expected_probability = (n_ones / m) ** k
|
expected_probability = (n_ones / m) ** k
|
||||||
@ -95,7 +97,7 @@ def test_probability(m=64, n=20):
|
|||||||
|
|
||||||
assert (
|
assert (
|
||||||
abs(expected_probability - fail_rate) <= 0.05
|
abs(expected_probability - fail_rate) <= 0.05
|
||||||
) # 5% margin calculated experiementally
|
) # 5% absolute margin calculated experiementally
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user