在填字游戏中找到一个字

时间:2016-03-06 16:11:33

标签: python arrays

编写一个名为capitalize_word_in_crossword的函数,它接受一个二维字符列表(如填字游戏)和一个字符串(字)作为输入参数。此函数搜索2d列表的行和列以查找该单词的匹配项。如果找到匹配项,则此函数将二维列表中匹配的字符大写并返回列表。如果未找到匹配项,则此函数只返回原始的二维列表而不进行修改。

示例1:如果调用该函数,如下所示:

enter image description here

文本:

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)

然后你的函数应该返回:

enter image description here

请注意,上面的列表是二维填字游戏的表示,如下所示。

enter image description here

示例2:如果调用函数如下所示:

enter image description here

文本:

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)

然后你的函数应该返回:

enter image description here

请注意,上面的列表是二维填字游戏的表示,如下所示。

enter image description here

注意:如果找到水平匹配和垂直匹配,则只选择水平匹配。例如,在上述情况下,存在从[2,1]开始的水平匹配,并且还存在从[1,0]开始的垂直匹配。请注意,只有水平匹配中的字符才能在返回的列表中大写。

我试过这个只能找到水平单词的代码。

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

不知道该怎么做。请帮忙。

4 个答案:

答案 0 :(得分:1)

crosswords=[['s', 'd', 'o', 'g'], ['c', 'u', 'c', 'm'], ['a', 'x', 'a', 't'], ['t', 'e', 't', 'k']]
words=['cat', 'dog']

def capitalize_word_in_crossword(crosswords, words):
    for rownum, row in enumerate(crosswords):
        for word in words:
            find_index=''.join(row).lower().find(word)
            if find_index>0:
                for i in range(find_index, len(word)+1):
                    crosswords[rownum][i]=crosswords[rownum][i].upper()

    for colindex in range(len(crosswords[0])):
        for word in words:
            colvalues=[row[colindex] for row in crosswords]
            find_index=''.join(colvalues).lower().find(word)
            if find_index>0:
                for i in range(find_index, len(word)+1):
                    crosswords[i][colindex]=crosswords[i][colindex].upper()
    return crosswords

print("Input: "+str(crosswords))
print("Output: "+str(capitalize_word_in_crossword(crosswords, words)))

输出:

$ python a.py 
Input: [['s', 'd', 'o', 'g'], ['c', 'u', 'c', 'm'], ['a', 'x', 'a', 't'], ['t', 'e', 't', 'k']]
Output: [['s', 'D', 'O', 'G'], ['C', 'u', 'C', 'm'], ['A', 'x', 'A', 't'], ['T', 'e', 'T', 'k']]
$
第一行中的

DOG和第二列和第三列中的CAT为大写。

第一个循环只是逐行获取所有内容并找到单词的索引,并使所有索引中的所有内容都大写,直到单词的长度。

第二个循环在将所有列1到1后完成相同的操作。

此外,上面的示例可以缩放以查找跨行和列的多个单词。

编辑1:将所有内容都放在函数中

编辑2:感谢海森堡。我找到了一些错过的东西并通过在检查子字符串时引入临时步骤lower()来修复它,因为某些字母已经是大写的(由于之前的迭代)

编辑3:再次感谢海森堡。我错过了这一点。我刚刚使用正常的填字游戏匹配来解决它。这是更正的版本,它维护每个单词的布尔标志,然后在垂直匹配时进行检查。

crosswords=[['s', 'd', 'o', 'g'], ['c', 'u', 'c', 'm'], ['a', 'c', 'a', 't'], ['t', 'e', 't', 'k']]
words=['cat', 'dog']
horizontal_match_found=[False, False]
word_index=0

def capitalize_word_in_crossword(crosswords, words):
    for rownum, row in enumerate(crosswords):
        word_index=0
        for word in words:
            find_index=''.join(row).lower().find(word)
            if find_index>0:
                for i in range(find_index, len(word)+1):
                    crosswords[rownum][i]=crosswords[rownum][i].upper()

                if not horizontal_match_found[word_index]:
                    horizontal_match_found[word_index]=True
            word_index+=1

    for colindex in range(len(crosswords[0])):
        word_index=0
        for word in words:
            if not horizontal_match_found[word_index]:
                colvalues=[row[colindex] for row in crosswords]
                find_index=''.join(colvalues).lower().find(word)
                if find_index>0:
                    for i in range(find_index, len(word)+1):
                        crosswords[i][colindex]=crosswords[i][colindex].upper()
            word_index+=1

    return crosswords

print("Input: "+str(crosswords))
print("Output: "+str(capitalize_word_in_crossword(crosswords, words)))

输出:

$ python a.py 
Input: [['s', 'd', 'o', 'g'], ['c', 'u', 'c', 'm'], ['a', 'c', 'a', 't'], ['t', 'e', 't', 'k']]
Output: [['s', 'D', 'O', 'G'], ['c', 'u', 'c', 'm'], ['a', 'C', 'A', 'T'], ['t', 'e', 't', 'k']]
$

注意(0基于索引):由于我们在第2行中匹配cat,因此我们在第0列和第2列中有 NOT 大写cat。 / p>

答案 1 :(得分:0)

def find_word_horizontal(crosswords, word):
    l=[]
    for row in crosswords:
        join_list = "".join(row)
        if join_list.find(word) != -1:
            print([crosswords.index(row), row.index(word[0])])
            return [crosswords.index(row), row.index(word[0])]

def find_word_vertical(crosswords,word):
    l=[]
    for i in range(len(crosswords[0])):
            l.append(''.join([row[i] for row in crosswords]))
            for line in l:
                print(line)
                if word in line:
                    row_index=i
                    column_index=line.index(word[0])
                    print([column_index,row_index])
                    return [column_index,row_index]


def capitalize_word_in_crossword(crosswords,word):
     if find_word_horizontal (crosswords,word) or find_word_vertical(crosswords,word) is not None:

我在这里工作。获取'cat'并将其大写

crosswords=[['s','d','o','g'],['c','u','c','m'],['a','x','a','t'],['t','e','t','k']]
word='cat'
print(capitalize_word_in_crossword(crosswords,word))

答案 2 :(得分:0)

def find_word_horizo​​ntal(填字游戏,单词):     L = []     对于填字游戏中的行:         join_list ="" .join(row)         如果join_list.find(word)!= -1:             return [crosswords.index(row),row.index(word [0])]

def find_word_vertical(填字游戏,单词):     L = []     for in in range(len(crosswords [0])):             l.append('' .join([row [i] for cross in rowwords]))             对于l行:                 如果排成一行:
                    ROW_INDEX =我                     与Column_Index = line.index(字[0])                     return [column_index,row_index]

def capitalize_word_in_crossword(填字游戏,单词):

 index_cap = find_word_horizontal(crosswords,word)
 if index_cap is not None:
    i,j=index_cap
 else:
    index_cap = find_word_vertical(crosswords,word)
    if index_cap is not None:
       i,j=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 row_index==i+w and col_index==j:
                      crosswords[row_index][col_index] = (crosswords[row_index][col_index]).upper()
                   else:                     
                      crosswords[row_index][col_index] = (crosswords[row_index][col_index])

       return crosswords

纵横字谜= [[' S'' d'' O'' G&#39],[' C& #39;,' U'' C'' M'],[' A'' X' ' A'' T&#39],[' T'' E'' T'&# 39; K']] 字='猫' 打印(capitalize_word_in_crossword(纵横字谜,字))

它有正确的结果

=============== RESTART:/Users/valdemarpavesi/Documents/32.py =============== [[' s' d',' o',' g'],[' C',& #39; u'' c',' m'],[' A',' x',' a','],[''' e',''' k&#39 ;]]

  
    
      

    
  

但是有一个与return

相关的错误

函数返回的变量类型为:NoneType

预期是:列表

答案 3 :(得分:0)

现在是正确的。

我有水平/垂直

的缩进问题和拆分索引
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)