问题描述
编写一个名为capitalize_word_in_crossword的函数,该函数接受二维字符列表(如填字游戏)和字符串(单词)作为输入参数.此功能搜索2d列表的行和列,以查找单词的匹配项.如果找到匹配项,则此函数将二维列表中的匹配字符大写并返回列表.如果找不到匹配项,则此函数将直接返回原始的二维列表,而无需进行任何修改.
Write a function named capitalize_word_in_crossword that accepts a 2-dimensional list of characters (like a crossword puzzle) and a string (word) as input arguments. This function searches the rows and columns of the 2d list to find a match for the word. If a match is found, this functions capitalizes the matched characters in 2-dimensional list and returns the list. If no match is found, this function simply returns the original 2-dimensional list with no modification.
示例1:如果如下所示调用函数:
Example 1: If the function is called as shown below:
文字:
crosswords=[['s','d','o','g'],['c','u','c','m'],['a','x','a','t'],['t','e','t','k']]
word='cat'
capitalize_word_in_crossword(crosswords,word)
然后您的函数应返回:
请注意,上面的列表代表了二维填字游戏,如下所示.
Notice that the above list is a representation for a 2-dimensional crossword puzzle as shown below.
示例2:如果如下所示调用函数:
Example 2: 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'
capitalize_word_in_crossword(crosswords,word)
然后您的函数应返回:
请注意,上面的列表代表了二维填字游戏,如下所示.
Notice that the above list is a representation for a 2-dimensional crossword puzzle as shown below.
注意:如果同时找到水平和垂直匹配,则仅选择水平匹配.例如,在上述情况下,水平匹配从[2,1]开始,垂直匹配也从[1,0]开始.请注意,在返回的列表中,只有水平匹配的字符才能大写.
Note: If both a horizontal and a vertical match is found then only select the horizontal match. For example, in the above case there is a horizontal match starting at [2,1] and there is also a vertical match starting at [1,0]. Notice that only the characters in the horizontal match should be capitalized in the returned list.
我尝试过此代码,该代码只能找到水平单词.
I have tried this code which only finds the horizontal words.
def find_word_horizontal(crosswords,word):
list1=[]
row_index = -1
column_index = -1
refind=''
for row in crosswords:
index=''
for column in row:
index= index+column
list1.append(index)
for find_word in list1:
if word in find_word:
row_index = list1.index(find_word)
refind = find_word
column_index = find_word.index(word)
ret = [row_index,column_index]
if row_index!= -1 and column_index != -1:
return ret
不知道该怎么做.请帮忙.
Don't know what to do further. Please help.
推荐答案
现在是正确的.
我有缩进问题和水平/垂直分割索引
i had indentation problem and split index for horizontal/vertical
def capitalize_word_in_crossword(crosswords,word):
i_v=-1
j_v=-1
i_h=-1
j_h=-1
index_cap = find_word_horizontal(crosswords,word)
if index_cap is not None:
i_h,j_h=index_cap
else:
index_cap = find_word_vertical(crosswords,word)
if index_cap is not None:
i_v,j_v=index_cap
for row_index in range(len(crosswords)):
for col_index in range(len(crosswords[row_index])):
for w in range(len(word)):
if i_h is not -1:
if row_index==i_h and col_index==j_h+w:
crosswords[row_index][col_index] = (crosswords[row_index][col_index]).upper()
if i_v is not -1:
if row_index==i_v+w and col_index==j_v:
crosswords[row_index][col_index] = (crosswords[row_index][col_index]).upper()
else:
crosswords[row_index][col_index] = (crosswords[row_index][col_index])
return (crosswords)
这篇关于在填字游戏中找到一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!