End of Summer - hours only!Up to 80% off on all courses and bundles.-Close
Scrabble Time

Instruction

Great! Now, on to the first exercise.

Exercise

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.

Stuck? Here's a hint!

First, create a copy of the list of letters passed as an argument:

letters_list_copy = list(letters_list)

Then iterate over the letters of a word:

for letter in word:
  ...

And remove a given letter from the copy if it is there. Otherwise, return False. If the loop finishes without returning False, then return True.