Created
March 20, 2018 21:31
-
-
Save grocid/155d01f51491e8093ca012036e6dc345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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