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
'''
return word in wordlist
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'
'''
mk_str=''
for letter in board[row_index]:
mk_str +=letter
return mk_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'
'''
mk_c=''
for i in board:
mk_c += i[column_index]
return mk_c
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[0])):
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.
checks whether a word that a player guessed occurs
in any of the rows or columns of the board.
Precondition: board has at least one row and one column.
>>> board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
True
'''
mk_str= make_str_from_row(board, row_index)
for i in word:
if word[i]== mk_str:
return True
return False
Can anybody guide me through this!
def board_contains_word(board, word):
'''(list of list of str, str) -> bool
Return True if and only if word appears in board.
checks whether a word that a player guessed occurs
in any of the rows or columns of the board.
Precondition: board has at least one row and one column.
>>> board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
True
'''
mk_str= make_str_from_row(board, row_index)
for i in word:
if word[i]== mk_str:
return True
return False

New Topic/Question
Reply



MultiQuote




|