Question

In problem 3, you will write a function, findvertically (), which will determine whether or not a given word exists in a wordReturn value (Output) • Return: o If word is found, return a dictionary containing the row and column indexes at which the wo6 ############################################ #### DO NOT CHANGE CODE INSIDE THIS BOX #### 9 # Direction constants 10 DIR U38 39- def findVertically(): # Locates a word occuring vertically in the word search puzzle, either downwards or upwards # @p

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def findVertically(puzzle, word):

global DIR_U, DIR_D

# If the length of word is greater than
# the puzzle no need to check
if len(word) == 0 or len(word) > len(puzzle):
return None
# Taking the transpose of the puzzle so that,
# we can process the words horizontally.
# Each row in puzzle becomes column in puzzle_transpose.
# Each column in puzzle becomes row in puzzle_transpose.
puzzle_transpose = [list(i) for i in zip(*puzzle)]
    
for row in puzzle_transpose:


# Row in puzzle_transpose is column of puzzle,
# hence the row index of puzzle_transpose gives column index in puzzle
# Column in puzzle_transpose is row of puzzle,
# hence the column index of puzzle_transpose gives row index in puzzle.
col_index = puzzle_transpose.index(row)
    
# Find word in row of puzzle_transpose in down direction.
row_str = ''.join(row)
row_index = row_str.find(word)
# Find word in row of puzzle_transpose in up direction by,
# searching for the reverse of word.
rev_row_index = row_str.find(word[::-1])

if row_index >= 0:
return {"row": row_index, "column":col_index, "direction": DIR_D}
elif rev_row_index >= 0:
# Correcting the index as the direction is from bottom to up.
return {"row": len(puzzle) - rev_row_index, "column":col_index, "direction": DIR_U}


return None

Please refer to the below screenshot of the above code if, there are any indent issues.

def findvertically(puzzle, word): # If the length of word is greater than # the puzzle no need to check if len(word) == 0 orThe output of the above script is as follows:

puzzle = [0, W, E, R], W, 0, R, D], [z, R, C, v], [V, N, N, M]] print(findvertically (puzzle,

If you have any queries please free to comment.

Add a comment
Know the answer?
Add Answer to:
In problem 3, you will write a function, findvertically (), which will determine whether or not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • C++ Programming help to write a code with the components: Randomly generate a 5x5 grid of...

    C++ Programming help to write a code with the components: Randomly generate a 5x5 grid of random letters. Create an interface to allow for the entry of a candidate word. If the word if found, the user interface will indicate this, if not, it will also inform the user. Allow for multiple words on the same grid to be guessed. Also allow for the ability to generate a new grid. Letters are considered to be touching if they are horizontally,...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints...

    Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints filename before the words filename. The starter file will be expecting them in that order. Lab#4's main method is completely written. Do not modify main. Just fill in the methods. Main will load a large arrays of int, and then load a large array of Strings. As usual the read loops for each file will be calling a resize method as needed. Once the...

  • Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry...

    Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem​: Each entry in the dictionary is a pair: (word, meaning). Word is a one-word string, meaning can be a string of one or more words (it’s your choice of implementation, you can restrict the meaning to one-word strings). The dictionary is case-insensitive. It means “Book”, “BOOK”, “book” are all the same . Your dictionary application must provide its operations through the following menu (make sure that...

  • Anybody Know how to Solve this Problem Thank you very much // ***** 1. This method...

    Anybody Know how to Solve this Problem Thank you very much // ***** 1. This method has been coded as an example /** Fills the array with random numbers between 50 and 80 * The instance variable named intArray is the integer array to be * filled with values */ public void fillValues( ) { Random rand = new Random( ); for ( int row = 0; row < intArray.length; row++ ) { System.out.print( row + "\t" ); for (...

  • in MATLAB code/syntax please 3) (Chap 7 problem) Write a function newwords that will receive a...

    in MATLAB code/syntax please 3) (Chap 7 problem) Write a function newwords that will receive a word in a character vector as an input argument. It will then randomly scramble the letters and return the result. Here is an example of calling the function: >> newwords ('exceptional) ans 'tponexlicae

  • 60 points, Complete javadocs documentation required Be sure to submit all files (.java and dictio...

    60 points, Complete javadocs documentation required Be sure to submit all files (.java and dictionary.txt) required to run your program Background Boggle is a word game using a plastic grid of lettered dice, in which players attempt to find words in sequences of adjacent letters. The dice are randomly arranged in the grid, and players have 90 seconds to form as many words as possible from adjacent top-facing letters For example, the word SUPER is spelled in the gameboard to...

  • 3. Write the function find sorted elt whose input is a vector sorted in increasing order...

    3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....

  • Write a C++ program to simulate a stream path. In this problem you can make the...

    Write a C++ program to simulate a stream path. In this problem you can make the following assumptions 1. The elevation grid will be a n row, m column grid of integers, where n and m are constant. 2. Once the stream origin location is specified, then the stream flows to whichever of the four adjacent locations in the grid represents the largest descent. 3. If more than one of the adjacent locations have the largest descent, then the water...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT