
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if a word exists in a grid or not in Python
Suppose, we have a grid or a matrix of words. We have to check whether a given word is present in the grid or not. The word can be found in four ways, horizontally left and right and vertically up and down. If we can find the word we return True, otherwise False.
So, if the input is like
p | g | h | s | f |
y | k | d | g | h |
t | k | g | h | i |
h | n | s | j | s |
o | j | f | g | h |
n | r | t | y | u |
input_str = "python", then the output will be True.
To solve this, we will follow these steps −
- Define a function find_grid() . This will take matrix, input_str, row_pos, col_pos, row_count, col_count, degree
- if degree is same as size of input_str , then
- return True
- if row_pos < 0 or col_pos < 0 or row_pos >= row_count or col_pos >= col_count, then
- return False
- if matrix[row_pos, col_pos] is same as input_str[degree], then
- temp := matrix[row_pos, col_pos]
- replace elements of matrix[row_pos, col_pos] with '#'
- result := find_grid(matrix, input_str, row_pos - 1, col_pos,
- row_count, col_count, degree + 1) bitwise or
- find_grid(matrix, input_str, row_pos + 1, col_pos, row_count,
- col_count, degree + 1) bitwise or
- find_grid(matrix, input_str, row_pos, col_pos - 1, row_count,
- col_count, degree + 1) bitwise or
- find_grid(matrix, input_str, row_pos, col_pos + 1, row_count, col_count, degree + 1))
- replace elements of matrix[row_pos, col_pos] with temp
- return result
- otherwise,
- return False
- if degree is same as size of input_str , then
- From the main function/method, do the following −
- if length of input_str > row_count * col_count, then
- return False
- for row in range 0 to row_count, do
- for col in range 0 to col_count, do
- if matrix[row, col] is same as input_str[0], then
- if (find_grid(matrix, input_str, row, col, row_count, col_count, 0) is True), then
- return True
- if (find_grid(matrix, input_str, row, col, row_count, col_count, 0) is True), then
- if matrix[row, col] is same as input_str[0], then
- for col in range 0 to col_count, do
- return False
Example
Let us see the following implementation to get better understanding −
def find_grid(matrix, input_str, row_pos, col_pos, row_count, col_count, degree) : if (degree == len(input_str)) : return True if (row_pos < 0 or col_pos < 0 or row_pos >= row_count or col_pos >= col_count) : return Fals if (matrix[row_pos][col_pos] == input_str[degree]) : temp = matrix[row_pos][col_pos] matrix[row_pos].replace(matrix[row_pos][col_pos], "#") result = (find_grid(matrix, input_str, row_pos - 1, col_pos, row_count, col_count, degree + 1) |find_grid(matrix, input_str, row_pos + 1, col_pos, row_count, col_count, degree + 1) |find_grid(matrix, input_str, row_pos, col_pos - 1, row_count, col_count, degree + 1) |find_grid(matrix, input_str, row_pos, col_pos + 1, row_count, col_count, degree + 1)) matrix[row_pos].replace(matrix[row_pos][col_pos], temp) return result else : return False def solve(matrix, input_str, row_count, col_count) : if (len(input_str) > row_count * col_count) : return False for row in range(row_count) : for col in range(col_count) : if (matrix[row][col] == input_str[0]) : if (find_grid(matrix, input_str, row, col, row_count, col_count, 0)) : return True return False word_grid = ["pghsf", "ykdgh", "tkghi", "hnsjs", "ojfgh", "nrtyu"] print(solve(word_grid, "python", 6, 5))
Input
["pghsf", "ykdgh", "tkghi", "hnsjs", "ojfgh", "nrtyu"],"python"
Output
True
- Related Articles
- How to check if a file exists or not using Python?
- How to check if a file exists or not in Java?
- How to check if a given word is a Python keyword or not?
- Check if a table is empty or not in MySQL using EXISTS
- How to check if a value exists in an R data frame or not?
- Check if a string is Isogram or not in Python
- How can I check whether a field exists or not in MongoDB?
- How to check whether a data frame exists or not in R?
- C# Program to check whether a directory exists or not
- Java Program to check whether a file exists or not
- How to check if a key exists in a Python dictionary?
- Check if a number is Primorial Prime or not in Python
- Check if a PHP cookie exists and if not set its value
- Check if a File exists in C#
- Check if a number is an Achilles number or not in Python

Advertisements