In Python word search, searching diagonally, printing result of where word starts and ends

2024/9/20 13:29:30

I have a friend of mine tutoring me in learning Python and he gave me this project where a user will read a word search into the program and the file includes a list of words that will be in the word search. I have to search for these words and some of the words run diagonally. If I find the word, I must print in what row and what column (the coordinates) the word starts and the word ends. I've only been learning Python 2 weeks so I'm confused, how would I search for a word diagonally and get the starting point and ending point of a word? The sample word search is down below and the words to search are with it. I have worked through it and spent 3 days on it and nothing has come of it.

Word Search

HGAMONIHRA
AOMOKAWONS
NFROLBOBDN
ARFSIHCAGE
LNIEEWONOK
GOLFUNDTHC
KOCATAOHBI
AMRERCGANH
SLGFAMALLC
ALLIGATORX

Words to search

CAT
DOG
ALLIGATOR
CHICKEN
FROG
Answer

This is more of a brute force problem, however there are more efficient techniques available but keeping in mind that you are new to this field I won't suggest you to focus on algorithmic part, So first of all we will create a function called search_diagonal which will take 3 arguments as starting_point, mesh, length_of_word and you can do some pretty stuff inside that function depending upon the arguments passed.

One you have 3 arguments you can then easily propagate diagonally as:

MESH = ["HGAMONIHRA", "AOMOKAWONS", "NFROLBOBDN", "ARFSIHCAGE", 
"LNIEEWONOK", "GOLFUNDTHC", "KOCATAOHBI", "AMRERCGANH", "SLGFAMALLC", 
"ALLIGATORX"]def search_diagonal(starting_point, MESH, length_of_word):new_word1 = ""new_word2 = ""new_word3 = ""new_word4 = ""for i in xrange(length_of_word):#Propagating in SE directionnew_word1+=MESH[starting_point[0]+i][starting_point[1]+i]for i in xrange(length_of_word):#Propagating in NE directionnew_word2+=MESH[starting_point[0]+i][starting_point[1]-i]for i in xrange(length_of_word):#Propagating in NW directionnew_word3+=MESH[starting_point[0]-i][starting_point[1]-i]for i in xrange(length_of_word):#Propagating in SW directionnew_word4+=MESH[starting_point[0]-i][starting_point[1]+i]return new_word1, new_word2, new_word3, new_word4

However there is a need to handle a lot of exception cases like index out of range etc. but this must give you a rough idea of how this problem can be solved.

https://en.xdnf.cn/q/119323.html

Related Q&A

Selenium python : element not interactable

I am trying to scrape information from this website example website I need to get the version 2021 and search by code. Here is my code: from selenium import webdriver from selenium.webdriver.chrome.opt…

Date into matplotlib graph

How can I use a date from a Sqlite database on the x-axis to make a bar graph with matplotlib?If I convert the date to unix timestamp the graph works, but I would like to get something like this: http…

Non blocking IO - Programming model

In non blocking IO programming model, a thread blocked on data available channels, as shown below, in python,while True:readers, _, _ = select.select([sys.stdin, sock], [], []) # blocked select()for re…

How to globally change xticks label relplot in Seaborn

I am facing an issue to globally changed the x-tick label for plot render using the relplot The idea was to change the int to string x-tick label for both plot. The desired label is [a,b,c,d] However, …

How to solve MatplotlibDeprecationWarning: scipy.stats.norm.pdf warning?

I am using matplotlib in my Python code. I got following warning: xxx.py:88: MatplotlibDeprecationWarning: scipy.stats.norm.pdfy = 100 * mlab.normpdf(bin_middles, mu, sigma)*bin_width I was wondering…

time data 42:53.700 does not match format %H:%M:%S.%f (match)

I am trying to convert a column in string format to DateTime format, However, I am getting the following error, could somebody please help? The error:time data 42:53.700 does not match format %H:%M:%S…

How can I import .py file? [duplicate]

This question already has answers here:Adding a directory to sys.path with pathlib(4 answers)Closed last year.Below is my code: from pathlib import Path import os import sys sys.path.insert(0, os.path.…

python: convenient way to create list of strings of repeated elements

How can I create a list like this:["a","a","a",... (repeating "a" a hundred times") "b","b","b",(repeating "b" a hun…

An accurate python sleep function

Ive tried time.sleep(), but its accuracy is total garbage. Consider this loop, for instance:for i in range(10000000):print(i)sleep(.334)Watch the numbers it prints. If its anything like my computer, it…

How to map python dictionary key values to each other?

Suppose we have two dictionaries as below: dict_a_to_b = {2:4, 6:9, 9:3} dict_a_to_c = {2: 0.1, 6:0.2, 9: 0.8}How to map these two dictionaries to make dict_c_to_b in python? dict_c_to_b = {0.1:4, 0.2…