From 9306376de25aef7a90f37aa35ccd19246886d453 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Thu, 9 Sep 2021 15:47:30 +0200 Subject: [PATCH] Module 18: Preventing Duplicates (Python code) --- a.py | 48 ++++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/a.py b/a.py index 2e214b6..7b99fd9 100644 --- a/a.py +++ b/a.py @@ -50,11 +50,9 @@ class Words: class Library: def __init__(self): - # master vector of word - self.words_ = Words() + self.words_ = Words() # master vector of word self.counts_ = {} - # hash table - self.word_map_ = {} + self.word_map_ = {} # hash table # Returns NULL if can't find any matches to the given pattern def FindWord(self, s): @@ -64,7 +62,6 @@ class Library: 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_: @@ -75,7 +72,6 @@ class Library: 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)}") @@ -86,15 +82,12 @@ class Library: def CreatePatternHash(self, w): len_w = len(w) 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, max_size): @@ -109,11 +102,10 @@ class Library: def DebugBuckets(self): for i, (k,v) in enumerate(self.word_map_.items()): - print(f"[{i}] {len(v)}")# {self.word_map_[i]}") + print(f"[{i}] {len(v)}") lib = Library() - ## ---------------------------------------------------------------------------- ##-Attr class Attr: @@ -243,14 +235,12 @@ class Grid: if not self.in_bounds(p): return startp = copy(p) - #print(f"SPAN START: {startp}") len_ = 0 keep_going = True while (keep_going and not self.is_block(p)): keep_going = self.NextStopAtWrap(p, vert) len_ += 1 - #print(f"END OF SPAN!!! len={len_}") self.sp.append(Spans(startp, len_, vert)) def FillSpans(self): @@ -260,7 +250,6 @@ class Grid: 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())) @@ -301,6 +290,13 @@ class Solver: def __init__(self): pass + + def ExistsInSet(self, set_, s): + return s in set_ + + def AddToSet(self, set_, s): + assert(not self.ExistsInSet(set_, s)) + set_.add(s) def Solve(self, grid): print(f"Solving this grid") @@ -309,9 +305,6 @@ class Solver: def Loop(self, grid, depth): depth += 1 - #if depth > 3: - #print(f"Aborting loop because depth={depth}") - #return empty_slots = [] partial_slots = [] # these are the ones we want to work on @@ -330,17 +323,19 @@ class Solver: num_empty = len(empty_slots) num_partial = len(partial_slots) num_full = len(full_slots) - # print(f"empty = {num_empty}") - # print(f"partial = {num_partial}") - # print(f"full = {num_full}") # need to check that all words so far are valid! for slot in full_slots: - #print(f"CHECKING {slot.pattern} if it is a word") if not lib.IsWord(slot.pattern): - #print(" --> NOT! ABORT") return + # need to check that all words are unique! no duplicates allowed. + my_set = set() + for slot in full_slots: + if self.ExistsInSet(my_set, slot.pattern): + return + self.AddToSet(my_set, slot.pattern) + if (num_partial == 0 and num_empty == 0): print("SOLUTION!!") grid.Print() @@ -351,20 +346,13 @@ class Solver: def CommitSlot(self, grid, slot, depth): grid = deepcopy(grid) - # print(f"COMMIT slot {slot}") - # print(f"Possible word choices for this slot are:") words = lib.FindWord(slot.pattern) if words: for w in words: - #print(f"{' '.join(words)}") - #print(f" Committing '{w}'!") grid.WriteString(slot.span, w) - #print("New grid is:") - #grid.Print() self.Loop(grid, depth) - #else: - #print("NO MATCHES to pattern") + ## ---------------------------------------------------------------------------- if __name__ == "__main__": grid = Grid("MY GRID")