There is a dictionary d
of letters and frequencies which represents a hand in a game similar to scrabble. If the letters in word
are contained within d
then the frequencies are changed or the letter is removed (if the value == 0) and the function update
returns True
, otherwise d
is unchanged and the function returns `False':
d = {'a': 1, 'p': 2, 'c': 1, }
dCopy = d.copy()
matching_lets = 0
def update():
for let in word:
if not let in dCopy:
return False
else:
if dCopy[let] == 1:
del dCopy[let]
else:
dCopy[let] -= 1
d = dCopy
return True
word = 'pap'
print update()
This is part of problem set 5 from the EDX course MITx 6.00.1, Introduction to Computer Science and Programming Using Python