私は数年の開発者ですが、アルゴリズムのスキルを練習しています。グリッド検索(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つだけのコメントのみで書いたもの、本質的にどれ。 – Chris
ニースのヒント。公正であるために、私はここにリンクを貼り付けたと誓った。 – PedroFTW
あなたがしようとしていることを説明するためにリンクを投稿することは、実際には十分ではありません。なぜなら、私は別のサイトに行って自分でそれを把握したくないからです。質問のポイントと改善が必要と思われる分野について説明してください。 –