网格搜索(HackerRank) - Python

时间:2016-04-25 18:38:47

标签: python algorithm matrix

我是开发人员几年但我练习算法技能。

我在HackerRank中面对"The Grid Search",即使我能解决它,我想知道这是否是一个不错的方法。

PS:我试图用简单的指令来完成大部分工作,开发整个逻辑而不是使用预先构建的函数。我的目标是改善我的逻辑思维,而不是我对语言魔法的了解。

#!/bin/python3

import sys


t = int(input().strip())
for a0 in range(t):
    R,C = input().strip().split(' ')
    R,C = [int(R),int(C)]
    G = []
    G_i = 0
    for G_i in range(R):
       G_t = list(input().strip())
       G.append(G_t)
    r,c = input().strip().split(' ')
    r,c = [int(r),int(c)]
    P = []
    P_i = 0
    for P_i in range(r):
       P_t = list(input().strip())
       P.append(P_t)

    mIsEqual = False

    #For each line of the matrix
    for a1 in range(0,len(G) - (len(P)-1)):
        #For each column of the given line
        for a2 in range(0,len(G[a1]) - (len(P[0])-1)):
            #If the top left value of the pattern matches the current value of the matrix, try to match it
            if(P[0][0] == G[a1][a2]):
                #If the pattern 'fits' horizontally in the matrix, try to match it
                if(len(P[0]) <= (len(G[a1]) - a2)):
                    #If the pattern 'fits' vertically in the matrix, try to match it
                    if(len(P) <= (len(G) - a1)):
                        #Match every single field of the pattern to the given area of the matrix.
                        for a3 in range(0,len(P)):
                            for a4 in range(0,len(P[0])):
                                #If the fields are equal mIsEqual is true
                                if(P[a3][a4] == G[a3+a1][a4+a2]):
                                    mIsEqual = True
                                else:
                                #If the fields are not equal stop matching this area of the matrix.
                                    mIsEqual = False
                                    break
                            #If one field in a line was not equal, stop matching this area of the matrix.
                            if(mIsEqual == False):
                                break
                    #If, after matching the whole area with the pattern mIsEqual is still true, the pattern is there.
                    if(mIsEqual):
                        break
        #If the pattern was found in the previous line, no need to keep this going.
        if(mIsEqual):
            break

    if(mIsEqual == True):
        print("YES")
    else:
        print("NO")

我正在寻找改进此脚本的任何建议,或者,如果您认为它完全错误,那么这就不是一个好方法。

谢谢!

1 个答案:

答案 0 :(得分:0)

在代码审查网站上这样做会更好。

您所描述的算法看起来大致正确。但是,如果你精神上把事情分解成函数,那么它会更容易理解。这也将删除一些重复的代码,并且还减少了在代码和赞誉中编写相同内容所需的量。

将您所写的内容与以下未经测试的代码进行比较。这基本上是你写的只有函数被破坏而且每个函数只有一个注释:

#!/bin/python3
import sys


# Read the dimensions then a matrix from stdin
def read_matrix():
    R,C = input().strip().split(' ')
    R,C = [int(R),int(C)]
    M = []
    M_i = 0
    for M_i in range(R):
       M_t = list(input().strip())
       M.append(M_t)
    return M


# See if P can be found anywhere in G.
def found_match_in(G, P):
    for a1 in range(0,len(G) - (len(P)-1)):
        for a2 in range(0,len(G[a1]) - (len(P[0])-1)):
            if found_match_at(G, P, a1, a2):
                return True
    return False


# See if P can be found in G at location (a1, a2).
def found_match_at(G, P, a1, a2):
    for a3 in range(0,len(P)):
        for a4 in range(0,len(P[0])):
            if G[a1+a3][a2+a4] != P[a3][a4]:
                return False
    return True


t = int(input().strip())
for a0 in range(t):
    G = read_matrix()
    P = read_matrix()
    if found_match_in(G, P):
        print "YES"
    else:
        print "NO"