seems not found anyone know how to do it ?
#!/bin/python3
import mathimport os
import randomimport re
import sys
## Complete the 'search_words' function below.
## The function is expected to return a STRING_ARRAY.
# The function accepts following parameters:# 1. STRING_ARRAY letters
# 2. STRING_ARRAY words#
def search_words(letters, words):
rows = len(letters) cols = len(letters[0])
def search(i, j, word, di, dj):
for k in range(len(word)): ni, nj = i + di*k, j + dj*k
if not (0 <= ni < rows and 0 <= nj < cols and letters[ni][nj] == word[k]): return False
return True
# Define the directions: horizontal right, vertical down, diagonal right-down, diagonal right-up directions = [
(0, 1), # horizontal right (1, 0), # vertical down
(1, 1), # diagonal right-down (-1, 1) # diagonal right-up
]
result = [] for word in words:
found = False for i in range(rows):
for j in range(cols): if letters[i][j] == word[0]:
for di, dj in directions: if search(i, j, word, di, dj):
found = True break
if found: break
if found: break
result.append("YES" if found else "NO") return result
if name == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
B = int(first_multiple_input[0])
K = int(first_multiple_input[1])
letters = []
for _ in range(B):
letters_item = input() letters.append(letters_item)
T = int(input().strip())
words = []
for _ in range(T):
words_item = input() words.append(words_item)
result = search_words(letters, words)
fptr.write('\n'.join(result))
fptr.write('\n')
fptr.close()
send this as .py we cant read it in chat ✨
Обсуждают сегодня