Guest User

Untitled

a guest
Mar 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. board = '''
  2. m a i n v f
  3. a n a f d d
  4. s m i t h a
  5. i r o n r v
  6. e m o z a i
  7. a e e t a d
  8. '''
  9.  
  10. words = '''
  11. ezra
  12. david
  13. sam
  14. smith
  15. iron
  16. fire
  17. '''
  18.  
  19. # use this to get a dictionary involved.
  20. #words = open('dictionary.txt', 'rt').read()
  21.  
  22. BOARD = {}
  23.  
  24. x = 0
  25. y = 0
  26. for line in board.strip().split('\n'):
  27. y += 1
  28. x = 0
  29. for char in line:
  30. char = char.lower()
  31. if char in 'abcdefghijklmnopqrstuvwxyz':
  32. x += 1
  33. BOARD[x,y] = char
  34.  
  35. WORDS = set()
  36.  
  37. for line in words.strip().split('\n'):
  38. word = line.strip().lower()
  39. if len(word) >= 3:
  40. WORDS.add(word)
  41.  
  42. DIREC = (
  43. (0,-1),
  44. (1,-1),
  45. (1,0),
  46. (1,1),
  47. (0,1),
  48. (-1,1),
  49. (-1,0),
  50. (-1,-1),
  51. )
  52.  
  53. PREFX = set()
  54.  
  55. FOUND = set()
  56.  
  57. for word in WORDS:
  58. prefix = ''
  59. for c in word[:-1]:
  60. prefix += c
  61. PREFX.add(prefix)
  62.  
  63. # For each position on the board
  64. for x,y in BOARD:
  65.  
  66. # For each direction of 8
  67. for xx,yy in DIREC:
  68. x1 = x #cur pos
  69. y1 = y #cur pos
  70. word = '' #cur word
  71.  
  72. while True:
  73.  
  74. # Check if off board
  75. if (x1,y1) not in BOARD:
  76. break
  77.  
  78. # Get character at current pos
  79. word += BOARD[x1,y1]
  80.  
  81. # If it is a word, add it
  82. if word in WORDS:
  83. FOUND.add(word)
  84.  
  85. # If this is not a prefix, then bail out
  86. if word not in PREFX:
  87. break
  88.  
  89. # Increment current position based on direction
  90. x1 += xx
  91. y1 += yy
  92.  
  93. print(FOUND)
Add Comment
Please, Sign In to add comment