Posts

Showing posts from November, 2018

Longest word chain from a list of words

Image
up vote 27 down vote favorite 4 So, this is a part of a function I'm trying to make. I don't want the code to be too complicated. I have a list of words, e.g. words = ['giraffe', 'elephant', 'ant', 'tiger', 'racoon', 'cat', 'hedgehog', 'mouse'] The idea of the word chain sequence is for the next word to begin with the letter that the last word ended in. (Edit: Each word cannot be used more than once. Other than that there are no other constraints.) I want the output to give the longest word chain sequence, which in this case is: ['hedgehog', 'giraffe', 'elephant', 'tiger', 'racoon'] I'm not really sure how to do it, I had different attempts at trying this. One of them... This code finds the word cha