Created
September 12, 2016 00:56
-
-
Save mkowoods/2cd7c7c1de35cad21e8e38bb791c9705 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import os | |
os.chdir('/Users/mwoods/Desktop') | |
# ----------------- | |
# User Instructions | |
# | |
# In this problem, you will define a function, boggle_words(), | |
# that takes a board as input and returns a set of words that | |
# can be made from the board according to the rules of Boggle. | |
def get_words(board, idx, N, pre_indices = ('', set()), min_length = 3, results = set()): | |
pre, seen_indices = pre_indices | |
if pre in WORDS and len(pre) >= min_length: | |
results.add(pre) | |
if pre in PREFIXES: | |
#print pre, neighbors(idx, N) | |
for neighbor_idx in neighbors(idx, N): | |
if neighbor_idx not in seen_indices: | |
tmp_indices = seen_indices.copy() | |
tmp_indices.add(neighbor_idx) | |
get_words(board, | |
neighbor_idx, | |
N, | |
pre_indices = (pre+board[neighbor_idx], tmp_indices), | |
min_length = min_length, | |
results = results) | |
return results | |
def boggle_words(board, minlength=3): | |
"Find all the words on this Boggle board; return as a set of words." | |
# your code here | |
N = size(board) | |
BOGGLE_PLAYS = set([]) | |
for i, c in enumerate(board): | |
get_words(board, i, N, pre_indices=(board[i], set([i])), results = BOGGLE_PLAYS) | |
return BOGGLE_PLAYS | |
def Board(text): | |
"""Input is a string of space-separated rows of N letters each; | |
result is a string of size (N+2)**2 with borders all around.""" | |
rows = text.split() | |
N = len(rows) | |
rows = [BORDER*N] + rows + [BORDER*N] | |
return ''.join(BORDER + row + BORDER for row in rows) | |
def size(board): | |
return int(len(board)**0.5) | |
def neighbors(i, N): | |
return (i-N-1, i-N, i-N+1, i-1, i+1, i+N-1, i+N, i+N+1) | |
BORDER = '|' | |
def display(board): | |
"Return a string representation of board, suitable for printing." | |
N = size(board) | |
return '\n'.join(board[i:i+N] for i in range(0, N**2, N)) | |
# ------------ | |
# Helpful functions | |
# | |
# You may find the following functions useful. These functions | |
# are identical to those we defined in lecture. | |
def prefixes(word): | |
"A list of the initial sequences of a word, not including the complete word." | |
return [word[:i] for i in range(len(word))] | |
def readwordlist(filename): | |
"Return a pair of sets: all the words in a file, and all the prefixes. (Uppercased.)" | |
wordset = set(open(filename).read().upper().split()) | |
prefixset = set(p for word in wordset for p in prefixes(word)) | |
return wordset, prefixset | |
WORDS, PREFIXES = readwordlist('words4k.txt') | |
def test(): | |
b = Board('XXXX TEST XXXX XXXX') | |
assert b == '|||||||XXXX||TEST||XXXX||XXXX|||||||' | |
assert display(b) == """ | |
|||||| | |
|XXXX| | |
|TEST| | |
|XXXX| | |
|XXXX| | |
||||||""".strip() | |
assert boggle_words(b) == set(['SET', 'SEX', 'TEST']) | |
assert neighbors(20, 6) == (13, 14, 15, 19, 21, 25, 26, 27) | |
assert len(boggle_words(Board('TPLER ORAIS METND DASEU NOWRB'))) == 317 | |
assert boggle_words(Board('PLAY THIS WORD GAME')) == set([ | |
'LID', 'SIR', 'OAR', 'LIS', 'RAG', 'SAL', 'RAM', 'RAW', 'SAY', 'RID', | |
'RIA', 'THO', 'HAY', 'MAR', 'HAS', 'AYS', 'PHI', 'OIL', 'MAW', 'THIS', | |
'LAY', 'RHO', 'PHT', 'PLAYS', 'ASIDE', 'ROM', 'RIDE', 'ROT', 'ROW', 'MAG', | |
'THIRD', 'WOT', 'MORE', 'WOG', 'WORE', 'SAID', 'MOR', 'SAIL', 'MOW', 'MOT', | |
'LAID', 'MOA', 'LAS', 'MOG', 'AGO', 'IDS', 'HAIR', 'GAME', 'REM', 'HOME', | |
'RED', 'WORD', 'WHA', 'WHO', 'WHOM', 'YID', 'DRAW', 'WAG', 'SRI', 'TOW', | |
'DRAG', 'YAH', 'WAR', 'MED', 'HIRE', 'TOWARDS', 'ORS', 'ALT', 'ORE', 'SIDE', | |
'ALP', 'ORA', 'TWA', 'ERS', 'TOR', 'TWO', 'AIS', 'AIR', 'AIL', 'ERA', 'TOM', | |
'AID', 'TOG', 'DIS', 'HIS', 'GAR', 'GAM', 'HID', 'HOG', 'PLAY', 'GOA', 'HOW', | |
'HOT', 'WARM', 'GOT', 'IRE', 'GOR', 'ARS', 'ARM', 'ARE', 'TOWARD', 'THROW']) | |
return 'tests pass' | |
print test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment