fork download
  1. class Solution:
  2. def find_index(self, search_list, symbol):
  3. result_list = []
  4. for i, v in enumerate(search_list):
  5. for j, v2 in enumerate(v):
  6. if v2 == symbol:
  7. result_list.append((i, j))
  8. return result_list
  9.  
  10. def next_index(self, current_index, previous_index):
  11. i, j = current_index
  12. all_neighbours = [(i-1, j), (i, j+1), (i+1, j), (i, j-1)]
  13. return [x for x in all_neighbours if 0<=x[0]<=3 and 0<=x[1]<=3 and x!=previous_index]
  14.  
  15. def find_word(self, word, start_position):
  16. previous_position = [start_position,]
  17. def check_matrix(word, start_position, next_letter=1):
  18. print('previous_position')
  19. print(previous_position)
  20. #nonlocal previous_position
  21. for neighbour_position in self.next_index(start_position, previous_position[-1]):
  22. i, j = neighbour_position
  23. print('position')
  24. print(neighbour_position)
  25. print('next_letter')
  26. print(next_letter)
  27. if board[i][j] == word[next_letter]:
  28. print('board[i][j]')
  29. print(board[i][j])
  30. print('word[next_letter]')
  31. print(word[next_letter])
  32. print('next_letter')
  33. print(next_letter)
  34. print('len(word)-1')
  35. print(len(word)-1)
  36.  
  37. if next_letter == len(word)-1:
  38. return True
  39. else:
  40. print(board[i][j])
  41. next_letter += 1
  42. check_matrix(word, neighbour_position, next_letter)
  43. previous_position.append(neighbour_position)
  44. else:
  45. continue
  46. return False
  47. return check_matrix(word, start_position)
  48. #check_matrix(word, start_position)
  49.  
  50. def findWords(self, board, words):
  51. result_words = []
  52. for word in words:
  53. print('-------------')
  54. print('word')
  55. print(word)
  56. print('word[0]')
  57. print(word[0])
  58. for start in self.find_index(board, word[0]):
  59. print('start')
  60. print(start)
  61. print('find_word')
  62. print(self.find_word(word, start))
  63. if self.find_word(word, start):
  64. result_words.append(word)
  65. return result_words
  66.  
  67. board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]]
  68. words = ["oath","pea","eat","rain"]
  69.  
  70. Solution().findWords(board, words)
Success #stdin #stdout 0.04s 9592KB
stdin
Standard input is empty
stdout
-------------
word
oath
word[0]
o
start
(0, 0)
find_word
previous_position
[(0, 0)]
position
(0, 1)
next_letter
1
board[i][j]
a
word[next_letter]
a
next_letter
1
len(word)-1
3
a
previous_position
[(0, 0)]
position
(0, 2)
next_letter
2
position
(1, 1)
next_letter
2
board[i][j]
t
word[next_letter]
t
next_letter
2
len(word)-1
3
t
previous_position
[(0, 0)]
position
(0, 1)
next_letter
3
position
(1, 2)
next_letter
3
position
(2, 1)
next_letter
3
board[i][j]
h
word[next_letter]
h
next_letter
3
len(word)-1
3
position
(1, 0)
next_letter
2
False
previous_position
[(0, 0)]
position
(0, 1)
next_letter
1
board[i][j]
a
word[next_letter]
a
next_letter
1
len(word)-1
3
a
previous_position
[(0, 0)]
position
(0, 2)
next_letter
2
position
(1, 1)
next_letter
2
board[i][j]
t
word[next_letter]
t
next_letter
2
len(word)-1
3
t
previous_position
[(0, 0)]
position
(0, 1)
next_letter
3
position
(1, 2)
next_letter
3
position
(2, 1)
next_letter
3
board[i][j]
h
word[next_letter]
h
next_letter
3
len(word)-1
3
position
(1, 0)
next_letter
2
-------------
word
pea
word[0]
p
-------------
word
eat
word[0]
e
start
(1, 0)
find_word
previous_position
[(1, 0)]
position
(0, 0)
next_letter
1
position
(1, 1)
next_letter
1
position
(2, 0)
next_letter
1
False
previous_position
[(1, 0)]
position
(0, 0)
next_letter
1
position
(1, 1)
next_letter
1
position
(2, 0)
next_letter
1
start
(1, 3)
find_word
previous_position
[(1, 3)]
position
(0, 3)
next_letter
1
position
(2, 3)
next_letter
1
position
(1, 2)
next_letter
1
board[i][j]
a
word[next_letter]
a
next_letter
1
len(word)-1
2
a
previous_position
[(1, 3)]
position
(0, 2)
next_letter
2
position
(2, 2)
next_letter
2
position
(1, 1)
next_letter
2
board[i][j]
t
word[next_letter]
t
next_letter
2
len(word)-1
2
False
previous_position
[(1, 3)]
position
(0, 3)
next_letter
1
position
(2, 3)
next_letter
1
position
(1, 2)
next_letter
1
board[i][j]
a
word[next_letter]
a
next_letter
1
len(word)-1
2
a
previous_position
[(1, 3)]
position
(0, 2)
next_letter
2
position
(2, 2)
next_letter
2
position
(1, 1)
next_letter
2
board[i][j]
t
word[next_letter]
t
next_letter
2
len(word)-1
2
-------------
word
rain
word[0]
r
start
(2, 3)
find_word
previous_position
[(2, 3)]
position
(1, 3)
next_letter
1
position
(3, 3)
next_letter
1
position
(2, 2)
next_letter
1
False
previous_position
[(2, 3)]
position
(1, 3)
next_letter
1
position
(3, 3)
next_letter
1
position
(2, 2)
next_letter
1