Skip to content

Instantly share code, notes, and snippets.

@d1p
Created April 17, 2020 15:56
Show Gist options
  • Save d1p/b8b0478dad60c402258d0e4af9b94cf4 to your computer and use it in GitHub Desktop.
Save d1p/b8b0478dad60c402258d0e4af9b94cf4 to your computer and use it in GitHub Desktop.
valid_words = ['chair', 'racket', 'touch', 'height', 'tunic']
chain = []
def chain_words(available_words:[], current_word: str):
chain.append(current_word)
available_words.remove(current_word)
for word in available_words:
if word.startswith(current_word[-1]):
chain_words(available_words, word)
def is_chain(words: []) -> bool:
chain_words(words, words[0])
if len(chain) < len(words):
return False
for i in range(0, len(chain)):
if i != len(chain) - 1:
if chain[i][-1] != chain[i + 1][0]:
return False
else:
if chain[i][-1] != chain[0][0]:
return False
return True
print(is_chain(valid_words))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment