Skip to content

Instantly share code, notes, and snippets.

@JuliusNM
Last active June 25, 2019 13:05
Show Gist options
  • Save JuliusNM/9d28930e89ab304550949a01566bd84f to your computer and use it in GitHub Desktop.
Save JuliusNM/9d28930e89ab304550949a01566bd84f to your computer and use it in GitHub Desktop.
Compound word exercise
# Assume we have a list of words from the English dictionary, like:
# english_words = ["water","big","apple","watch","banana","york","amsterdam","orange","macintosh","bottle","book"]
# And another long list of string to process, write a function to identify "compound words" and return them:
# output: ["applewatch","bigbook","waterbottle"]
english_words = ["water","big","apple","watch","banana","york","amsterdam","orange","macintosh","bottle","book"]
inputWords = ["paris","applewatch","ipod","amsterdam","bigbook","orange","waterbottle"]
def find_compound_words(english_words, input):
return [input_word for input_word in inputWords for word in english_words if word in input_word and input_word[len(word):] in english_words]
print(find_compound_words(english_words,inputWords))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment