You are on page 1of 4

"""A board is a list of list of str.

For example, the board


ANTT
XSOB
is represented as the list
[['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']]
A word list is a list of str. For example, the list of words
ANT
BOX
SOB
TO
is represented as the list
['ANT', 'BOX', 'SOB', 'TO']
"""
def is_valid_word(wordlist, word):
""" (list of str, str) -> bool
Return True if and only if word is an element of wordlist.
>>> is_valid_word(['ANT', 'BOX', 'SOB', 'TO'], 'TO')
True
"""
if word in wordlist:
return True
return False
def make_str_from_row(board, row_index):
""" (list of list of str, int) -> str
Return the characters from the row of the board with index row_index
as a single string.
>>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
'ANTT'
"""
row_str = ''
for char in board[row_index]:
row_str = row_str + char
return row_str
def make_str_from_column(board, column_index):
""" (list of list of str, int) -> str
Return the characters from the column of the board with index column_index
as a single string.
>>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
'NS'
"""
col_str = ''
for lst in board:

col_str = col_str + lst[column_index]


return col_str
def board_contains_word_in_row(board, word):
""" (list of list of str, str) -> bool
Return True if and only if one or more of the rows of the board contains
word.
Precondition: board has at least one row and one column, and word is a
valid word.
>>> board_contains_word_in_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']],
'SOB')
True
"""
for row_index in range(len(board)):
if word in make_str_from_row(board, row_index):
return True
return False
def board_contains_word_in_column(board, word):
""" (list of list of str, str) -> bool
Return True if and only if one or more of the columns of the board
contains word.
Precondition: board has at least one row and one column, and word is a
valid word.
>>> board_contains_word_in_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B'
]], 'NO')
False
"""
for column_index in range(len(board)):
if word in make_str_from_column(board, column_index):
return True
return False
def board_contains_word(board, word):
""" (list of list of str, str) -> bool
Return True if and only if word appears in board.
Precondition: board has at least one row and one column.
>>> board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
True
"""
rows_list = ''
cols_list = ''

for lst in board:


for char in lst:
rows_list = rows_list + char
counter = 0
while counter < len(board[0]):
for char in board:
cols_list = cols_list + board[board.index(char)][counter]
counter = counter + 1
if word in rows_list or word in cols_list:
return True
return False
def word_score(word):
""" (str) -> int
Return the point value the word earns.
Word length: < 3:
3-6:
7-9:
10+:

0
1
2
3

points
point per character for all characters in word
points per character for all characters in word
points per character for all characters in word

>>> word_score('DRUDGERY')
16
"""
total = 0
if len(word) < 3:
return total
elif len(word) >= 3 and
for char in word:
total = total +
elif len(word) >= 7 and
for char in word:
total = total +
elif len(word) >= 10:
for char in word:
total = total +

len(word) <= 6:
1
len(word) <= 9:
2
3

return total
def update_score(player_info, word):
""" ([str, int] list, str) -> NoneType
player_info is a list with the player's name and score. Update player_info
by adding the point value word earns to the player's score.
>>> update_score(['Jonathan', 4], 'ANT')
"""
player_info[1] = player_info[1] + word_score(word)
def num_words_on_board(board, words):

""" (list of list of str, list of str) -> int


Return how many words appear on board.
>>> num_words_on_board([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], ['ANT',
'BOX', 'SOB', 'TO'])
3
"""
return len(words)
def read_words(words_file):
""" (file open for reading) -> list of str
Return a list of all words (with newlines removed) from open file
words_file.
Precondition: Each line of the file contains a word in uppercase characters
from the standard English alphabet.
"""
file = open(words_file, 'r')
words_list = []
for line in file:
words_list.append(line[:-1])
return words_list
file.close()
def read_board(board_file):
""" (file open for reading) -> list of list of str
Return a board read from open file board_file. The board file will contain
one row of the board per line. Newlines are not included in the board.
"""
file = open(board_file, 'r')
board_list = []
for line in file:
line_list = ''
for char in line:
line_list = line_list + char
board_list.append([line_list[:-1]])
return board_list
file.close()

You might also like