def ToUpper(s): return s.upper() ## ---------------------------------------------------------------------------- ##-Point class Point: def __init__(self, r=0, c=0): self.row = r self.col = c def __str__(self): return f'({self.row},{self.col})' ## ---------------------------------------------------------------------------- ##-Span class Span: def __init__(self, p, l, v): self.point = p self.len = l self.vert = v def __str__(self): return f'[{self.point} len={self.len} vert={self.vert}]' ## ---------------------------------------------------------------------------- ##-Words class Words: def __init__(self): self.word = [] ## ---------------------------------------------------------------------------- ##-Library class Library: def __init__(self): # master vector of word self.words_ = Words() self.counts_ = {} # hash table self.word_map_ = {} # Returns NULL if can't find any matches to the given pattern def FindWord(self, s): print(list(key for key, values in self.word_map_.items() if s in values)) def IsWord(self, s): 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.word) if _len <= 18: 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(): # 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] def CreatePatternHash(self, w): len_w = len(w) if len_w > 7: return num_patterns = 1 << len_w # print(f"PATTERN HASH on {w}") self.word_map_[w] = [] for i in range(num_patterns): # print(f" {i}") tmp = list(w) for j in range(len_w): if ((i >> j) & 1): tmp[j] = "-" # print(f' {"".join(tmp)}') self.word_map_[w].append("".join(tmp)) def ReadFromFile(self, filename): with open(filename, 'r') as f: for line in f: line = ToUpper(line.rstrip()) self.words_.word.append(line) self.CreatePatternHash(line) print(f"Read {len(self.words_.word)} words from file '{filename}'") def DebugBuckets(self): for i, (k,v) in enumerate(self.word_map_.items()): print(f"[{i}] {len(v)}")# {self.word_map_[i]}") class Grid: def __init__(self, n): self.name = n self.lines = [] def rows(self): return len(self.lines) def cols(self): if self.lines == []: return 0 else: return len(self.lines[0]) def LoadFromFile(self, filename): with open(filename, 'r') as f: for line in f: #print(f"{line.rstrip()} ({len(line.rstrip())})") if not line.startswith('#'): self.lines.append(list(line.rstrip())) def Check(self): for s in self.lines: assert len(s) == self.cols() def Print(self): print(f"Grid: {self.name} " f"(rows={self.rows()}," f" cols={self.cols()})") for s in self.lines: print(f" {''.join(s)}") if __name__ == "__main__": lib = Library() lib.ReadFromFile("top_12000.txt") grid = Grid("MY GRID") grid.LoadFromFile("test") grid.Check() grid.Print() p1 = Point() p2 = Point(2, 1) print(f"Point1 is {p1}") print(f"Point2 is {p2}") s1 = Span(p1, 3, True) s2 = Span(p2, 5, False) print(f"Span1 is {s1}") print(f"Span2 is {s2}")