Create a function called can_build_word(letters_list, word)
that takes a list of letters and a word. The function should return True
if the word uses only the letters from the list. Like in real Scrabble, you cannot use one tile twice in the same word. For example, if a list of letters contains only one 'a'
, the function returns False
if the word contains more than one 'a'
. The function should also return False
if the word contains other letters than those provided in the list.
For example,
can_build_word(['a', 'b', 'e', 'g', 'l', 'r', 'z'], 'glare')
should return True
.
can_build_word(['a', 'i', 'n', 's', 'f', 'q', 'y'], 'sniff')
should return False
because there is only one 'f'
.
One thing to keep in mind: neither the provided letter list, nor the word should be changed by your function. It may be tempting to remove letters from the letters list to remove the used tiles. This should not be the case. Instead, you should copy the passed list. Simply use the list()
function and pass it another list:
word_list_copy = list(word_list)
This way we will safely operate on a copy without changing the contents of the original variable.