【问题标题】:Python Word Search SolverPython 单词搜索求解器
【发布时间】:2016-02-28 19:06:25
【问题描述】:

我只是不知道从哪里开始。我得到了下面的问题,我真的被困住了。我将非常感谢任何帮助。我的编码尝试如下所示。谢谢。

"编写一个函数,它接受一个二维字符列表和一个字符串(单词)作为输入参数。这个函数搜索二维列表的行以找到单词的匹配项。如果找到匹配项,则此函数返回一个包含匹配开始的行索引和列索引的列表,否则返回值 None(无引号)。

characters=[['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]
word='cat'

function(crosswords,word)

那么你的函数应该返回 [2,1]。

注意二维输入列表代表一个二维填字游戏,横向单词“cat”的起始索引为[2,1]

【问题讨论】:

  • 你说你的尝试在下面。我所看到的只是设置实际开始实施的代码。此外,您正在调用一个未显示的名为 function 的函数。有更多代码可以展示吗?
  • 您需要展示您尝试过的内容以及遇到的错误或错误。我们不会为你做作业。
  • 另外,您使用characters 作为矩阵名称,但尝试调用function(crosswords, word)。

标签: python multidimensional-array


【解决方案1】:

有趣的任务...我在 Python 方面处于相当初级的水平,所以如果你还没有这样做,我可以解决你一半的问题。

def function(crosswords, word):

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

字符=[['s','d','o','g'],['c','u','c','m'],['a','c' ,'a','t'],['t','e','t','k']] 字='猫' 打印(函数(字符,单词))

如果在数组中找到单词,则返回 TRUE,否则返回 FALSE。您需要将 'return True' 和 'return False' 替换为您需要返回的内容。

编辑:我制定了一个解决方案,它完成了工作;尽管这可能不是最好的方法。可能还有改进的余地。

【讨论】:

    【解决方案2】:
    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
    
    
    
    crosswords=[['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]
    word='cat'
    print(find_word_horizontal(crosswords,word))
    

    【讨论】:

    • 您应该在代码的每一行前面加上 4 个空格来表示用于格式化的代码块
    【解决方案3】:

    您好,与垂直而不是水平查找单词有何不同? - 该函数搜索二维列表的行以找到匹配的单词。

    【讨论】:

      【解决方案4】:
      def find_word_horizontal(crosswords,word):
          if not crosswords or not word:
              return None    
          number_of_rows=len(crosswords)
          for row_index in range (number_of_rows):
              temp_str=''
              for col_index in range(len(crosswords[0])):
                  temp_str=temp_str+crosswords[row_index][col_index]
              if temp_str.find(word)>=0:
                  return [row_index,temp_str.find(word)]
          return None
      

      【讨论】:

      • 请解释您的问题并标记上面代码为@sereno的编程语言
      【解决方案5】:
      def find_word_horizontal(crosswords,word):
          if not crosswords or not word : # if empty then return None
              return None
          for index,row in enumerate(crosswords):
              temp_str=''.join(row)
              if temp_str.find(word)>=0:
                  return [index,temp_str.find(word)]
          return None
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-15
        • 1970-01-01
        • 2019-03-05
        • 2021-12-11
        • 2015-07-28
        • 1970-01-01
        相关资源
        最近更新 更多