Skip to content

Instantly share code, notes, and snippets.

@ryantuck
Created December 31, 2016 02:06
Show Gist options
  • Save ryantuck/7af0490e9242fa3b99ef14c4f61784af to your computer and use it in GitHub Desktop.
Save ryantuck/7af0490e9242fa3b99ef14c4f61784af to your computer and use it in GitHub Desktop.
# define letters in puzzle
letters = [
'g', 'w', 'p', 'e', 'o', 'f',
'v', 'q', 'r', 'x', 's', 'z',
'm', 'c', 'i', 'k', 'u', 'd',
]
# unique words in puzzle 1
words_1 = [
'bus',
'cardinal',
'rye',
'acquire',
'even',
'also',
'silicon',
'sleigh',
'prose',
'merry',
'barley',
'scene',
'area',
'endurance',
'blur',
'nil',
'crocus',
'enough',
'copy',
'igloo',
'lee',
'guy',
]
# unique words in puzzle 2
words_2 = [
'fund',
'mad',
'hierarchy',
'gorilla',
'arc',
'fun',
'impose',
'ice',
'nail',
'submerge',
'huge',
'usher',
'dwell',
'mechanic',
'dry',
'carafe',
'high',
'cheese',
'impulse',
'scuba',
'inch',
'beg',
]
# function for finding which words in a list are made up of a given set of letters
def words_containing_letters(words, letters):
# define empty output list
output = []
# iterate through words
for word in words:
# define check here to keep track of whether word contains letters
check = True
# make copy of list of letters
tmp_letters = list(letters)
# iterate through letters in current word
for wl in word:
# check if letter is in list of letters
if wl in tmp_letters:
# if so, remove from master list of letters so we don't double-count
tmp_letters.remove(wl)
continue
else:
# otherwise, we failed, and break out of loop
check = False
break
# if all letters were found, add word to output list
if check:
output.append(word)
return output
# print output of both batches of words
print words_containing_letters(words_1, letters)
print words_containing_letters(words_2, letters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment