0% found this document useful (0 votes)
1 views2 pages

Draft Word Grid

The document contains a Python script that defines functions to read a list of valid words and a grid of letters from files. It includes functions to search for valid words horizontally and vertically in the grid, appending found words to a list. The main function orchestrates the process by calling the other functions and returning the sorted list of found words.

Uploaded by

visualrafi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Draft Word Grid

The document contains a Python script that defines functions to read a list of valid words and a grid of letters from files. It includes functions to search for valid words horizontally and vertically in the grid, appending found words to a list. The main function orchestrates the process by calling the other functions and returning the sorted list of found words.

Uploaded by

visualrafi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

def get_word_list():

valid_words = []
word_list_file = input()
file = open(word_list_file,"r")
for word in file:
valid_words.append(word.lower())
return valid_words

def read_letters_files():
grid = []
grid_of_letters_file = input()
file = open(grid_of_letters_file)
for row in file:
row_characters = row.split(" ")
grid.append(row_characters)
return grid

found_words = [] # initialize found word list outside funcitons

def search_horizontal(grid, valid_words):


for row in grid:
row_len = len(row)

for start in range(row_len):


# loop over possible lengths, all over len 3
for length in range(3, row_len - start + 1):
# concatenate elements within this loop
string = ''.join(row[start:start + length])
# check if the substring is a valid word
if string in valid_words:
found_words.append(string)

reversed_row = row[::-1]
for start in range(row_len):
for length in range(3, row_len - start + 1):
string = ''.join(reversed_row[start:start + length])
if string in valid_words:
found_words.append(string)

return sorted("\n".join(found_words))

def search_vertical(grid, valid_words):


num_cols = len(grid[0])
for col in range(num_cols):
# convert column to list
vertical_list = [row[col] for row in grid]
v_len = len(vertical_list)

# search top-to-bottom
for start in range(v_len):
for length in range(3, v_len - start + 1):
string = ''.join(vertical_list[start:start + length])
if string in valid_words:
found_words.append(string)

# search bottom-to-top
rev_vert_list = vertical_list[::-1]
for start in range(v_len):
for length in range(3, v_len - start + 1):
string = ''.join(rev_vert_list[start:start + length])
if string in valid_words:
found_words.append(string)

return sorted("\n".join(found_words))

def main():
valid_words = get_word_list()
grid = read_letters_files()
search_horizontal(grid, valid_words)
return sorted("\n".join(found_words))

main()

You might also like