如何在填字游戏中垂直查找单词 [英] how to find word vertically in a crossword
本文介绍了如何在填字游戏中垂直查找单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个函数,该函数接受一个二维 (2D) 字符列表(如填字游戏)和一个字符串作为输入参数,然后该函数必须搜索 2D 列表的列以找到一个词的匹配.如果找到匹配项,则该函数应返回一个包含匹配项开头的行索引和列索引的列表,否则应返回值 None.
I'm trying to write a function that accepts a 2-dimensional (2D) list of characters (like a crossword puzzle) and a string as input arguments, the function must then search the columns of the 2D list to find a match of the word. If a match is found, the function should then return a list containing the row index and column index of the start of the match, otherwise it should return the value None.
例如,如果函数被调用如下:
For example if the function is called as shown below:
crosswords = [['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]
word = 'cat'
find_word_vertical(crosswords,word)
那么函数应该返回:
[1,0]
推荐答案
def find_word_vertical(crosswords,word):
columns = []
finished = []
for col in range(len(crosswords[0])):
columns.append( [crosswords[row][col] for row in
range(len(crosswords))])
for a in range(0, len(crosswords)):
column = [crosswords[x][a] for x in range(len(crosswords))]
finished.append(column)
for row in finished:
r=finished.index(row)
whole_row = ''.join(row)
found_at = whole_row.find(word)
if found_at >=0:
return([found_at, r])
这篇关于如何在填字游戏中垂直查找单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文