Question: Word search game! Write a program (in python) that can solve a word search given to it in the form of a two-dimensional array. The

Word search game!

Write a program (in python) that can solve a word search given to it in the form of a two-dimensional array.

The number of cases is how many array/word sets you should expect

You will then print out the start and end positions for the word if it is found, and if the word is not found (does not exist in the array), you must report that to the user.

For example, if your main program receives the following standard input:

1 5 5 r g p g p k p o f z q d x s t v y r y j e w y t e dog

...you should print out the following:

Searching for "dog" in matrix 0 yields: Start pos: (2, 1) to End pos: (0, 3) 

Similarly, if your program receives the following input:

1 5 5 r g p g p k p o f z q d x s t v y r y j e w y t e cat

...you should print out the following:

Searching for "cat" in matrix 0 yields: The pattern was not found.

Here's what I have so far, I need help with getting the starting and ending positions.

def build_matrix(rows, cols): matrix = [] for i in range(rows): matrix.append([])

for user_char in matrix: user_data = input() x = user_data.split() for j in range(cols): user_char.append(x[j]) return matrix

def matrix_search(r, c, word, mat): for i in range(0, len(word)): for j in range(0, r): for k in range(0, c): if mat[j][k] == word[i]: break if i == 0: start = mat[j][k] elif i == len(word) - 1: end = mat[j][k] else: return None

return start, end

def main() -> None: test_case = int(input()) for num in range(test_case): space = input() rows_cols = input() split = rows_cols.split() rows = int(split[0]) cols = int(split[1]) word_search = build_matrix(rows, cols) word = input() match = matrix_search(rows, cols, word, word_search) if match is None: print('Searching for "' + word + '" in matrix', num, "yields:") print("The pattern was not found.") else: i, j = match print('Searching for "' + word + '" in matrix', num, "yields:") print("Start pos: ({0}, {1}) to End pos: ()".format(i, j))

if __name__ == "__main__": main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!