Module 10: Search the Library, Fast (Python code)

This commit is contained in:
David Doblas Jiménez 2021-07-23 12:48:17 +02:00
parent 0c75398111
commit 0a340c84e9
1 changed files with 18 additions and 35 deletions

53
a.py
View File

@ -7,67 +7,49 @@ class Words:
def __init__(self):
self.word = []
num_buckets = 1001
def bucket(s):
assert(len(s) != 0)
i = 0
for c in s:
i = (i * 217) + ord(c)
i = abs(i)
b = i % num_buckets
assert(b >= 0 and b < num_buckets)
return b
class Library:
def __init__(self):
# master vector of word
self.words = Words()
self.counts = {}
self.shelves = {}
for i in range(num_buckets):
self.shelves[i] = []
self.words_ = Words()
self.counts_ = {}
# hash table
self.word_map_ = {}
def IsWord(self, s):
for Words.word in self.shelves[bucket(s)]:
if s == Words.word:
return True
return False
return s in self.word_map_
def ComputeStats(self):
# assert self.counts == {}
for i in range(18):
self.counts[i] = []
for s in self.words:
_len = len(s)
self.counts_[i] = []
for s in self.words_:
_len = len(s.word)
if _len <= 18:
self.counts[_len-1].append(_len)
self.counts_[_len-1].append(_len)
def PrintStats(self):
print("Here are the counts of each word length")
for k,v in self.counts.items():
for k,v in self.counts_.items():
# print(v)
if k != 0:
print(f"[{k}] {len(v)}")
def GetWord(self, i):
assert (i >= 0 and i < len(self.words))
return self.words[i]
assert (i >= 0 and i < len(self.words_))
return self.words_[i]
def ReadFromFile(self, filename):
with open(filename, 'r') as f:
for line in f:
line = ToUpper(line.rstrip())
self.words.word.append(line)
self.shelves[bucket(line)].append(line)
print(f"Read {len(self.words.word)} words from file '{filename}'")
self.words_.word.append(line)
self.word_map_[line] = line
print(f"Read {len(self.words_.word)} words from file '{filename}'")
def DebugBuckets(self):
for i in range(len(self.shelves)):
print(f"[{i}] {len(self.shelves[i])}")
for i in range(len(self.word_map_.keys())):
print(f"[{i}]")# {self.word_map_[i]}")
class Grid:
@ -112,6 +94,7 @@ if __name__ == "__main__":
print(lib.IsWord("DOG"))
print(lib.IsWord("CAT"))
print(lib.IsWord("THANKS"))
print(lib.IsWord("TFAFAS"))
lib.DebugBuckets()
# grid = Grid("MY GRID")