Created
May 16, 2022 18:17
-
-
Save bigntallmike/7a67dbfa27cb5660e3162012b7ec997d to your computer and use it in GitHub Desktop.
Starting hamming database
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
#!/usr/bin/python3 -ttu | |
class hamming_db: | |
def __init__(self, itemlen): | |
self._itemlen = itemlen | |
self._itemlist = [] | |
# List of dictionaries, containing entries from itemlist that match | |
self._positiondb = [{} for _ in range(self._itemlen)] | |
def add(self, item): | |
if not len(item) == self._itemlen: | |
raise RuntimeError('Item is the wrong length') | |
self._itemlist.append(item) | |
lastitemidx = len(self._itemlist)-1 | |
# Now add it to the index: | |
for pos in range(self._itemlen): | |
c = item[pos] | |
posdict = self._positiondb[pos] | |
if not c in posdict: | |
posdict[c] = [] | |
posdict[c].append(lastitemidx) | |
self._positiondb[pos] = posdict | |
def main(): | |
SIZE=4 | |
DB = hamming_db(SIZE) | |
with open("/usr/share/dict/words", 'r') as words: | |
for i, word in enumerate(words): | |
if len(word) == SIZE: | |
DB.add(word) | |
print(DB._positiondb) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment