Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. class Trie(object):
  2.             def __init__(self):
  3.                 self.children = {}
  4.                
  5.             def insert(self, word):
  6.                 if word[0] not in self.children:
  7.                     self.children[word[0]] = Trie()
  8.                 l = len(word)
  9.                 if l == 1:
  10.                     return
  11.                 self.children[word[0]].__insert(word, 1, l)
  12.                
  13.             def __insert(self, word, i, l):
  14.                 if word[i] not in self.children:
  15.                     self.children[word[i]] = Trie()
  16.                 if l - i == 1:
  17.                     return
  18.                 self.children[word[i]].__insert(word, i+1, l)                
  19.            
  20.             def search(self, word):
  21.                 if word[0] not in self.children:
  22.                     return False
  23.                 l = len(word)
  24.                 if l == 1:
  25.                     return True
  26.                 return self.children[word[0]].__search(word, 1, l)
  27.            
  28.             def __search(self, word, i, l):
  29.                 if word[i] not in self.children:
  30.                     return False
  31.                 if l - i == 1:
  32.                     return True
  33.                 return self.children[word[i]].__search(word, i+1, l)
  34.            
  35.         from itertools import product
  36.         from collections import namedtuple
  37.         from copy import copy
  38.        
  39.         if type(board) is not list or len(board) == 0:
  40.             return []
  41.         if type(board[0]) is not list or len(board[0]) == 0:
  42.             return []
  43.         if type(words) is not list or len(words) == 0:
  44.             return []
  45.        
  46.         m = len(board)
  47.         n = len(board[0])
  48.        
  49.         trie = Trie()
  50.         words = [ list(w) for w in words ]
  51.        
  52.         for word in words:
  53.             trie.insert(word)
  54.            
  55.         Point = namedtuple('Point', ['x', 'y'])
  56.                
  57.        
  58.         def crawl(_letters, c_point, _points, output):
  59.             letters = copy(_letters)
  60.             x = c_point.x
  61.             y = c_point.y
  62.             letters.append(board[x][y])
  63.            
  64.             points = _points.copy()
  65.             points.add(c_point)
  66.            
  67.             if trie.search(letters):
  68.                 if letters in words:
  69.                     w = ''.join(letters)
  70.                     if w not in output:
  71.                         output.append(w)
  72.                    
  73.                
  74.                    
  75.                 # Make Points
  76.                 adjacent_points = []
  77.                 for p in [ Point(x-1,y), Point(x+1,y), Point(x,y-1), Point(x,y+1) ]:
  78.                     if (p not in points and
  79.                         m > p.x >= 0 and
  80.                         n > p.y >= 0):
  81.                             adjacent_points.append(p)
  82.                    
  83.                 for p in adjacent_points:
  84.                     crawl(letters, p, points, output)
  85.                    
  86.                 return False
  87.        
  88.         output = []
  89.         pointz = set()
  90.         for i,j in product(range(m), range(n)):
  91.             crawl([], Point(i,j), pointz, output)
  92.            
  93.         return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement