Assignment: Word and Math Puzzles
Learning Outcomes
• String manipulation, slicing, nested lists and list comprehensions, list membership test
• OOP: constructors, instance methods, class variables, abstract methods, single inheritance
• File input, exception handling
Introduction In this assignment, you are required to implement a single program (puzzle.py) for solving both word puzzles and math puzzles on a 2D game board. The program must define the following class hierarchy:
The superclass Puzzle is an abstract base class (ABC) which defines an abstract method called solve(). Two concrete subclasses named WordPuzzle and MathPuzzle are derived from Puzzle. They override the parent solve() method with their own versions for solving the puzzle with different logics – one is to locate all correct English words on the game board while another is to find the maximum product of a fixed-size list of adjacent numbers on the board.
Part A: Word Puzzles
Suppose that you are given a word puzzle represented by a 2D grid or array of alphabets. Solving the
puzzle means finding all correct words in the grid by checking against an English dictionary (a list of
words loaded from a text file). For example, given the following array in Figure 1(a), your program is
to find words from the dictionary that can be spelled out in the puzzle by starting at any character,
then moving in a straight line right or down. In other words, the program performs two scanning
phases: (1) horizontal scanning from left to right on every row, starting from row 0; and (2) vertical
scanning from top to bottom on every column, starting from column 0. Horizontal scanning is done
before vertical scanning. For example, in phase (1), the word “walk” is found at row 3 as Figure 1(b)
depicts; in phase (2), the same word “walk” is found in a vertical sense at column 1 in Figure 1(c). Word
matching in diagonal senses is not required in this program.
Get Free Quote!
374 Experts Online