word_search

py

School

University Of Arizona *

*We aren’t endorsed by this school

Course

120

Subject

Computer Science

Date

Apr 3, 2024

Type

py

Pages

4

Uploaded by MateClover22302

Report
""" File: word_search.py Author: Nicholas Brobeck Course: CSC 120 Purpose: This program performs a word search in a grid of letters. It reads a list of words and a grid of letters from specified files, searches for the words horizontally, vertically, and diagonally in the grid, and prints the found words. """ def get_word_list(word_list_filename): """ Read a list of words from a file. Parameters: - word_list_filename (str): The name of the file containing the list of words. Returns: - word_list: A list of words read from the file, with leading and trailing whitespaces removed. """ # Open the file in read mode file = open(word_list_filename, 'r') # Create a list of words by stripping leading and trailing whitespaces word_list = [word.strip() for word in file] # Close the file file.close() return word_list def read_letters_file(grid_filename): """ Read a grid of letters from a file. Parameters: - grid_filename (str): The name of the file containing the grid of letters. Returns: - list: A 2D list representing the grid of letters read from the file. """ # Open the file in read mode file = open(grid_filename, 'r') # Create a 2D list by splitting each line into individual letters grid = [list(line.split()) for line in file] # Close the file file.close() return grid def occurs_in(substr, word_list): """ Check if a substring occurs in a list of words. Parameters: - substr (str): The substring to check for in the list of words. - word_list (list): A list of words to search for the substring. Returns:
- bool: True if the substring is found (case-insensitive) in the word list, False otherwise. """ # Convert the substring and each word in the list to lowercase for # case-insensitive comparison substr_lower = substr.lower() for word in word_list: if word.lower() == substr_lower: return True # Return False if the substring is not found in the word list return False def horizontal_search(grid, word_list): """ Search for words horizontally in the given grid. Parameters: - grid (list): A 2D list representing the grid of letters. - word_list (list): A list of words to search for in the grid. Returns: - list: A list of confirmed words found horizontally in the grid. """ confirmed_words = [] # Return an empty list if the grid is empty if not grid or not grid[0]: return confirmed_words # Iterate through each row in the grid for row in grid: # Iterate through each possible substring in the row for i in range(len(row)): for j in range(i + 3, min(i + 3 + len(row), len(row) + 1)): word = ''.join(row[i:j]) # Check if the word or its reverse occurs in the word list if occurs_in(word, word_list): confirmed_words.append(word) elif occurs_in(word[::-1], word_list): confirmed_words.append(word[::-1]) return confirmed_words def vertical_search(grid, word_list): """ Search for words vertically in the given grid. Parameters: - grid (list): A 2D list representing the grid of letters. - word_list (list): A list of words to search for in the grid. Returns: - list: A list of confirmed words found vertically in the grid. """ confirmed_words = []
# Return an empty list if the grid is empty if not grid or not grid[0]: return confirmed_words # Iterate through each column in the grid for col in range(len(grid[0])): # Iterate through each possible substring in the column for i in range(len(grid) - 2): for j in range(i + 3, len(grid) + 1): word = ''.join(grid[x][col] for x in range(i, j)) # Check if the word or its reverse occurs in the word list if occurs_in(word, word_list): confirmed_words.append(word) elif occurs_in(word[::-1], word_list): confirmed_words.append(word[::-1]) return confirmed_words def diagonal_search(grid, word_list): """ Search for words diagonally in the given grid. Parameters: - grid (list): A 2D list representing the grid of letters. - word_list (list): A list of words to search for in the grid. Returns: - list: A list of confirmed words found diagonally in the grid. """ confirmed_words = [] # Return an empty list if the grid is empty if not grid or not grid[0]: return confirmed_words # Iterate through each position in the grid for i in range(len(grid)): for j in range(len(grid[0])): # Iterate through each possible substring in the diagonal direction for k in range(3, min(len(grid) - i, len(grid[0]) - j) + 1): word = ''.join(grid[i + x][j + x] for x in range(k)) # Check if the word or its reverse occurs in the word list if occurs_in(word, word_list): confirmed_words.append(word) elif occurs_in(word[::-1], word_list): confirmed_words.append(word[::-1]) return confirmed_words def print_words(words): """ Print a sorted and unique list of words. Parameters: - words (list): A list of words to be printed. """ for word in sorted(set(words)):
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
print(word) def main(): """ Perform a word search using the provided word list and letters grid. """ # Read the word list and letters grid from user input word_list = get_word_list(input()) letters_grid = read_letters_file(input()) all_words = [] # Perform word search in horizontal, vertical, and diagonal directions all_words.extend(horizontal_search(letters_grid, word_list)) all_words.extend(vertical_search(letters_grid, word_list)) all_words.extend(diagonal_search(letters_grid, word_list)) # Print the found words print_words(all_words) main()