Last active
November 16, 2024 17:26
-
-
Save Tusenka/5cb6cfb6bba7428f6a1583a8dc5c7ff5 to your computer and use it in GitHub Desktop.
My version of grid search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# | |
# Complete the 'gridSearch' function below. | |
# | |
# The function is expected to return a STRING. | |
# The function accepts following parameters: | |
# 1. STRING_ARRAY G | |
# 2. STRING_ARRAY P | |
# | |
def _is_pattern_in_rows(G, P, i, j): | |
for x in range (i+1, len(P)+i): | |
if G[x][j:len(P[x-i])+j]!=P[x-i]: | |
return False | |
return True | |
def _find_next_pattern(G, P): | |
for i in range(len(G)-len(P)+1): | |
j=-1 | |
while True: | |
j=G[i].find(P[0], j+1) | |
if j>=0: | |
yield [i, j] | |
else: | |
break | |
return | |
def gridSearch(G, P): | |
for x in _find_next_pattern(G, P): | |
if _is_pattern_in_rows(G, P, x[0], x[1]): | |
return 'YES' | |
return 'NO' | |
if __name__ == '__main__': | |
fptr = open(os.environ['OUTPUT_PATH'], 'w') | |
t = int(input().strip()) | |
for t_itr in range(t): | |
first_multiple_input = input().rstrip().split() | |
R = int(first_multiple_input[0]) | |
C = int(first_multiple_input[1]) | |
G = [] | |
for _ in range(R): | |
G_item = input() | |
G.append(G_item) | |
second_multiple_input = input().rstrip().split() | |
r = int(second_multiple_input[0]) | |
c = int(second_multiple_input[1]) | |
P = [] | |
for _ in range(r): | |
P_item = input() | |
P.append(P_item) | |
result = gridSearch(G, P) | |
fptr.write(result + '\n') | |
fptr.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment