Last active
August 29, 2015 14:07
-
-
Save dblotsky/87416ae42865d7348bf8 to your computer and use it in GitHub Desktop.
Happy Word Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# the word seach board, as a list of rows | |
# NOTE: | |
# rows must all be of the same length | |
BOARD = [ | |
"wcbqlqhtwgayj", | |
"bfhtgaycxqqtx", | |
"vrhczdtgayxfq", | |
"cvgaykcxwpfhd", | |
"tbqlqgayltvtg", | |
"sdwhfxcqjbqla", | |
"gflqtbgayrdhy", | |
"abfjvqhlvqfcb", | |
"ytqhzcwzshdtx", | |
"zvgayxljtlgmh", | |
"ljcqvjhtfqapq", | |
"pgaytpcqxvyjb", | |
] | |
HEIGHT = len(BOARD) | |
WIDTH = len(BOARD[0]) | |
# the set of strings that we can find on the BOARD | |
# NOTE: | |
# sets enforce uniqueness | |
strings = set() | |
# all unique words from the file words.txt | |
english = set(open("words.txt").read().split()) | |
def col(i, start=0, end=None): | |
""" | |
Return a section of a column from the board. | |
""" | |
return "".join(s[i] for s in BOARD[start:end]) | |
def row(i, start=0, end=None): | |
""" | |
Return a section of a row from the board. | |
""" | |
return BOARD[i][start:end] | |
def add(s): | |
""" | |
Add a string and its reverse to the set of found strings. | |
""" | |
strings.add(s) | |
strings.add(s[::-1]) | |
# find and add all horizontal strings | |
for r in range(HEIGHT): | |
for i in range(WIDTH): | |
for j in range(i, WIDTH): | |
add(row(r, i, j)) | |
# find and add all vertical strings | |
for c in range(WIDTH): | |
for i in range(HEIGHT): | |
for j in range(i, HEIGHT): | |
add(col(c, i, j)) | |
# get only the strings that are actual English words | |
words = strings.intersection(english) | |
# print out the words in sorted order | |
for word in sorted(words): | |
print word |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment