我正在尝试创建一个函数,该函数将从列表中的列表中获取对角线,也就是我的单词搜索难题的矩阵。
def get_left_up (start_x, start_y, board):
word = []
for row in range (start_x, -1,-1):
for col in range (start_y,-1, -1):
word.append(board[row][col])
word = ''.join(word)
return word
get_left_down (start_x, start_y, board):
word = []
for row in range (start_x,len(board)):
for col in range (start_y,-1,-1):
word.append(board[row][col])
word = ''.join(word)
return word
def get_right_up (start_x, start_y, board):
word= []
for row in range (start_x,-1,-1):
for col in range (start_y,len(board)):
word.append(board[row][col])
word = ''.join(word)
return word
def get_right_down (start_x, start_y, board):
word = []
for row in range (start_x, len(board)):
for col in range (start_y,len(board)):
word.append(board[row][col])
word = ''.join(word)
return word
到现在为止,我应该在其中工作的功能。但是,出了点问题。我打算输入与要搜索的单词匹配的第一个字母的坐标的参数,并将其从对角线的各个方向连接起来。
答案 0 :(得分:0)
对于右下角的对角线,您需要在同一迭代中增加x-和y-,而不是遍历特定行的所有列条目。
def get_right_down (start_x, start_y, board):
word = []
col = start_y
for row in range (start_x, len(board)):
if col >= len(board):
break
word.append(board[row][col])
col = col + 1
return ''.join(word)