In: Computer Science
Write a function checkvertical(board, word, row, col), which returns True if the word word can be added to the board starting at position board[row][col] and going down. The restrictions are (1) it has to fit entirely on the board, (2) it has to intersect and match with one or more non-blank letters on the board, and (3) it cannot change any letters on the board, other than blanks (that is, overlapping mismatches are not allowed).
My program isn't working :( Can anyone fix the program?
My program:
def checkvertical(board, word, row, col):
if len(word) > 20 - row:
return False
matchesoneletter = False
for k in range(len(word)):
wordletter = word[k]
boardletter = board[row + k][col]
if wordletter == boardletter:
matchesoneletter = True
if boardletter == blank:
continue
elif boardletter != wordletter:
return False
return matchesoneletter
def checkvertical(board, word, row, col):
#considering board size of 20 x 20 seeing your
code.
#if the len(word) doesn't fit vertically at that
position.
if len(word)>20-row:
return False
#else check for each letter in word matches with
the
#letter on the bord starting index(row) or there is
blank space
#if not so exit by returning false
#if it successfully runs till the end, it
implies,
#the word can be fit there.
for i in range(len(word)):
# here i am considering -1
representing blank space in your board
# if not so change that value
if word[i] == board[row+i][col] or
board[row+i][col]==-1:
continue
else:
return
False
return True
#refer above image for indentation.
# I am unable to understand the logic you want to
implement
# with your code. hence I am providing my code.
# If you have any doubt ask in the comments section
below.
# Also please leave your rating. Thank you!