Given an m by n grid of letters, and a list of words, find the location in the g
ID: 3656472 • Letter: G
Question
Given an m by n grid of letters, and a list of words, find the location in the grid where the word can be found. A word matches a straight, contiguous line of letters in the grid. The match could either be done horizontally (left or right) or vertically (up or down). We will not consider words that can be matched diagonally.
The input will be in a file called hidden.txt and your output will be written to a file called found.txt. The format of the input file will be as follows:
There will be k lines in your output file. Each line will have the word that you were search for followed by two integers i and j separated by one or more spaces. The numbers will be right justified and aligned. The number i gives the row and the number j the column of the first letter of the word that you were required to find. Rows and columns are numbered conventionally, i.e. the first row is 1 and the first column is 1. If you do not find a word in the grid then the values for i and j will be 0 and 0. Use the full power of the built-in functions associated with strings and lists.
hidden.txt
found.txt
BLIZZARD 0 0
BOOTS 8 1
CANDLE 4 5
COAT 5 9
DECEMBER 13 4
FIREPLACE 3 11
HILL 0 0
MITTENS 4 13
SEASON 14 5
SKATES 1 1
SLEIGHBELLS 0 0
SNOWMAN 2 5
SNOWSTORM 1 4
WINTER 0 0
Explanation / Answer
#python 2.6.1 def populate_matrix(rows, cols): import random seq = [chr(i) for i in range(ord('a'), ord('a')+cols)] matrix = [] for row in range(rows): array = [] for col in range(cols): array.append(random.choice(seq)) matrix.append(array) return matrix if __name__ == "__main__": rows = 10 columns = 20 matrix = populate_matrix(rows, columns) print ' ', for i in range( ord('A'), ord('A')+columns ): print chr(i), print print ' ', for i in range(columns): print '-', print for i in range(rows): print '%2d | ' % (i+1), for col in matrix[i]: print col, print
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.