Skip to content

Instantly share code, notes, and snippets.

@abali96
Created October 21, 2015 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abali96/a97648f7ed52ab1913d8 to your computer and use it in GitHub Desktop.
Save abali96/a97648f7ed52ab1913d8 to your computer and use it in GitHub Desktop.
board = [["o", "a", "g", "r"], ["s", "q", "n", "t"], ["e", "i", "s", "g"]]
def spell_all_words(board, word_dictionary):
words_found = list()
for row_number in range(len(board)):
for col_number in range(len(board[row_number])):
for i_offset in range(-1, 2): # get our adjacent characters
if not 0 <= row_number + i_offset < len(board):
continue
for j_offset in range(-1, 2):
if 0 <= col_number + j_offset < len(board[row_number]): # check if we aren't running off the end of the array
if not i_offset == j_offset == 0:
recursive_spell_all_words(board, word_dictionary, words_found, row_number, col_number, i_offset, j_offset, board[row_number][col_number])
def recursive_spell_all_words(board, word_dictionary, words_found, row, col, i, j, working_word):
print(working_word)
if working_word in word_dictionary:
words_found.append(working_word)
if row + i < 0 or row + i >= len(board) or col + j < 0 or col + j >= len(board[row]):
return
recursive_spell_all_words(board, word_dictionary, words_found, row + i, col + j, i, j, working_word + board[row + i][col + j])
spell_all_words(board, list()) # Pass in an empty list for the dictionary bc I don't have a dictionary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment