Skip to content

Instantly share code, notes, and snippets.

@grocid
Created March 20, 2018 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grocid/155d01f51491e8093ca012036e6dc345 to your computer and use it in GitHub Desktop.
Save grocid/155d01f51491e8093ca012036e6dc345 to your computer and use it in GitHub Desktop.
from hashlib import md5
class BloomFilter:
def __init__(self, m, k):
self.array = [False] * 2**m
self.k = k
def c(self, x):
f = lambda x, j: int(
md5(str(x) + str(j)).hexdigest(), 16
) % len(self.array)
return [f(x,i) for i in xrange(self.k)]
def add(self, x):
for d in self.c(x):
self.array[d] = True
def is_member(self, x):
return reduce(
(lambda x, y: x and y),
[self.array[d] for d in self.c(x)]
)
bloom = BloomFilter(10, 12)
bloom.add("muslimannen")
print bloom.is_member("godishuset")
print bloom.is_member("muslimannen")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment