PYTHON   36
updateHand
Guest on 26th August 2023 12:39:11 AM


  1.  
  2. def updateHand(hand, word):
  3.     """
  4.    Assumes that 'hand' has all the letters in word.
  5.    In other words, this assumes that however many times
  6.    a letter appears in 'word', 'hand' has at least as
  7.    many of that letter in it.
  8.  
  9.    Updates the hand: uses up the letters in the given word
  10.    and returns the new hand, without those letters in it.
  11.  
  12.    Has no side effects: does not modify hand.
  13.  
  14.    word: string
  15.    hand: dictionary (string -> int)    
  16.    returns: dictionary (string -> int)
  17.    """
  18.     # TO DO ... <-- Remove this comment when you code this function
  19.     output = hand.copy()
  20.     for letter in word:
  21.         if letter in output.keys():
  22.             output[letter] -= 1
  23.     return output

Raw Paste

Login or Register to edit or fork this paste. It's free.