2016-04-25 12 views
-1

私は数年の開発者ですが、アルゴリズムのスキルを練習しています。グリッド検索(HackerRank) - Python

私は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

:あなたが勃発機能や機能ごとに1つだけのコメントのみで書いたもの、本質的にどれ。 – Chris

+0

ニースのヒント。公正であるために、私はここにリンクを貼り付けたと誓った。 – PedroFTW

+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" 
+0

ねえ、ヒントありがとう。 – PedroFTW

関連する問題