Advertisement
Guest User

Helper funcitions Problem set 4 MITx6.00

a guest
Nov 3rd, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. #
  2. # Problem #2: Update a hand by removing letters
  3. #
  4. def updateHand(hand, word):
  5.     """
  6.    Assumes that 'hand' has all the letters in word.
  7.    In other words, this assumes that however many times
  8.    a letter appears in 'word', 'hand' has at least as
  9.    many of that letter in it.
  10.  
  11.    Updates the hand: uses up the letters in the given word
  12.    and returns the new hand, without those letters in it.
  13.  
  14.    Has no side effects: does not modify hand.
  15.  
  16.    word: string
  17.    hand: dictionary (string -> int)    
  18.    returns: dictionary (string -> int)
  19.    """
  20.     return dict((key, hand[key] - word.count(key)) for key in hand)
  21.  
  22.  
  23. def possible(word, hand):
  24.     """
  25.    Returns True if word which is assumed to to be correct is entirely
  26.    composed of letters in the hand. Otherwise, returns False.
  27.  
  28.    Does not mutate hand or wordList.
  29.  
  30.    word: string
  31.    hand: dictionary (string -> int)
  32.    """
  33.     return all(hand.setdefault(c, 0) >= word.count(c) for c in set(word))
  34.  
  35. #
  36. # Problem #3: Test word validity
  37. #
  38. def isValidWord(word, hand, wordList):
  39.     """
  40.    Returns True if word is in the wordList and is entirely
  41.    composed of letters in the hand. Otherwise, returns False.
  42.  
  43.    Does not mutate hand or wordList.
  44.  
  45.    word: string
  46.    hand: dictionary (string -> int)
  47.    wordList: list of lowercase strings
  48.    """
  49.     return word in wordList and possible(word, hand)
  50.  
  51.  
  52. #
  53. # Problem #4: Playing a hand
  54. #
  55.  
  56. def calculateHandlen(hand):
  57.     """
  58.    Returns the length (number of letters) in the current hand.
  59.    
  60.    hand: dictionary (string-> int)
  61.    returns: integer
  62.    """
  63.     return sum(hand[key] for key in hand)
  64.  
  65.  
  66.  
  67. def playHand(hand, wordList, n):
  68.  
  69.     """
  70.    Allows the user to play the given hand, as follows:
  71.  
  72.    * The hand is displayed.
  73.    * The user may input a word or a single period (the string ".")
  74.      to indicate they're done playing
  75.    * Invalid words are rejected, and a message is displayed asking
  76.      the user to choose another word until they enter a valid word or "."
  77.    * When a valid word is entered, it uses up letters from the hand.
  78.    * After every valid word: the score for that word is displayed,
  79.      the remaining letters in the hand are displayed, and the user
  80.      is asked to input another word.
  81.    * The sum of the word scores is displayed when the hand finishes.
  82.    * The hand finishes when there are no more unused letters or the user
  83.      inputs a "."
  84.  
  85.      hand: dictionary (string -> int)
  86.      wordList: list of lowercase strings
  87.      n: integer (HAND_SIZE; i.e., hand size required for additional points)
  88.      
  89.    """
  90.     total_score = 0  
  91.     while True:
  92.         print 'Current Hand:',
  93.         displayHand(hand)
  94.         word = raw_input('Enter word, or a "." to indicate that you are finished: ').lower()
  95.         if word == '.':
  96.             print "Goodbye!",
  97.             break
  98.         else:
  99.             if not isValidWord(word, hand, wordList):
  100.                 print "That is not a valid word. Please choose another word"
  101.             else:
  102.                 score = getWordScore(word, n)
  103.                 hand = updateHand(hand, word)
  104.                 total_score += score
  105.                 print '"%s" earned %i points. Total: %i points' % (word, score, total_score)
  106.                 if calculateHandlen(hand) == 0:
  107.                     print "\nRun out of letters.",
  108.                     break
  109.                 print
  110.                
  111.     # Game is over (ran out of letters or no more valid words), so tell user the total score and return it
  112.     print "Total score: %i points." % total_score
  113.     return total_score
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement