Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
CS Games 2023 Team A Application
PLEASE FILL IN THE FOLLOWING!
Full Name
Kelly Owenya
UWindsor Email
[email protected]
Application Form
Briefly explain how your solution works and how to run it
How it works:
My solution uses recursion, a stack, maps, and optimization to get an answer.
First, information about the dimensions of the given board is stored, and a history list is initialized to keep track of what cells have already been used.
Then, in order to avoid losing time recursively looking through inputs that won't work, I've included a way to catch those tedious inputs and immediately return False.
First, I populate two hashmaps: one with the actual word, and one with the characters in the board. They map a character to its frequency.
Then, I iterate through the dictionary of the word's frequencies, and if the word requires more of the same letter than the board has of that letter, it will be impossible for the word to exist within the board, so it can just return False immediately. Even in the case that a word has a new character that the board doesn't have at all, which normally raises a KeyError, it should still return False.
The last optimization is if the frequency of the first letter in the word is higher in the board than the last letter of the word, I reverse the word because the way my algorithm works, it's faster for a letter with less frequency to be the first one checked.
Now, it's time to explain the way my search algorithm works.
The first if statement is if we’ve just simply arrived at the last character of the word. Then, we can return True, because we’ve already validated that every other character of the word can be formed from the board recursively. (This is part of why the last optimization saves time.)
Otherwise, if the letter is out of bounds, not possible to be found, or has already been visited and used, it should return False. These are all things that would make a word impossible.
If those things aren’t encountered, we begin the recursive part of the function, because we’ve found a viable place for the desired letter and want to see if the next one can be found.
The coordinates of the letter are pushed onto the stack of the history (so it can’t be used again), and then the search algorithm is recursively called on every possible adjacent square to see if on at least one, the next letter of the word can be found. After that, the letter coordinates are simply popped off the stack, because we won’t be needing them anymore. Then, the result of the search is returned.
Search is called on every square of the board until one square reveals a path that leads to everything being found, which means the word exists in a valid way within the board. (This is the other part of why reversing the word can save time, because the algorithm will spend time exploring less paths.) Otherwise, if no viable paths are found, it returns False.
I also included an input parser which takes the format specified by the question and makes sure the program can interpret it (because it requires a 2D list and a string, and not just a multi-line string). You can add testcases either way as the given testcases show, just make sure you call *input_parser() if you use the way specified by the question!
How to run it:
Make sure your computer has Python 3 installed, and you can run it on any IDE of your choice!