Skip to content

Instantly share code, notes, and snippets.

@mrdmnd
Last active May 22, 2020 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdmnd/3b21da2ee0b4f4a6bf2f0d660dea4c97 to your computer and use it in GitHub Desktop.
Save mrdmnd/3b21da2ee0b4f4a6bf2f0d660dea4c97 to your computer and use it in GitHub Desktop.
Word Chain Homework Exercise
import sys
# Should return a set() of all words in the scrabble dictionary passed in at the given file path.
# You should implement this file loader.
def LoadWords(scrabble_dictionary_path):
pass
# Return a set() of ALL valid words from the input set `all_words` that differ by exactly one letter from `word`
# Hint: you may want to try using regular expressions here.
def FindAllNeighbors(word, all_words):
pass
# Use breadth first search to identify a path of legal words that link `input_word` and `target_word`.
# This function should return the words from the chain in a list.
def Chain(input_word, target_word, all_words):
pass
# FUN EXTENSION!
# Search for the pair of words S and T with the longest "shortest path" between them - which words are really hard to navigate between?
# Called from command line like "wordchain.py path_to_scrabble_dict.txt"
if __name__ == '__main__':
scrabble_dict_path = sys.argv[1]
all_words = LoadWords(scrabble_dict_path)
print(Chain("TAP", "TOP", all_words)) # Should print ["TAP", "TOP"] as these words are already separated by only one edit.
print(Chain("CAP", "TOP", all_words)) # Could print ["CAP", "TAP", "TOP"], or any other valid sequence of words between these two.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment