Guest User

ps3a

a guest
Oct 30th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.13 KB | None | 0 0
  1. # 6.00 Problem Set 3A Solutions
  2. #
  3. # The 6.00 Word Game
  4. # Created by: Kevin Luu <luuk> and Jenna Wiens <jwiens>
  5. #
  6. #
  7.  
  8. import random
  9. import string
  10.  
  11. VOWELS = 'aeiou'
  12. CONSONANTS = 'bcdfghjklmnpqrstvwxyz'
  13. HAND_SIZE = 7
  14.  
  15. SCRABBLE_LETTER_VALUES = {
  16.     'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10
  17. }
  18.  
  19. # -----------------------------------
  20. # Helper code
  21. # (you don't need to understand this helper code)
  22.  
  23. WORDLIST_FILENAME = "words.txt"
  24.  
  25. def load_words():
  26.     """
  27.    Returns a list of valid words. Words are strings of lowercase letters.
  28.    
  29.    Depending on the size of the word list, this function may
  30.    take a while to finish.
  31.    """
  32.     print "Loading word list from file..."
  33.     # inFile: file
  34.     inFile = open(WORDLIST_FILENAME, 'r', 0)
  35.     # wordlist: list of strings
  36.     wordlist = []
  37.     for line in inFile:
  38.         wordlist.append(line.strip().lower())
  39.     print "  ", len(wordlist), "words loaded."
  40.     return wordlist
  41.  
  42. def get_frequency_dict(sequence):
  43.     """
  44.    Returns a dictionary where the keys are elements of the sequence
  45.    and the values are integer counts, for the number of times that
  46.    an element is repeated in the sequence.
  47.  
  48.    sequence: string or list
  49.    return: dictionary
  50.    """
  51.     # freqs: dictionary (element_type -> int)
  52.     freq = {}
  53.     for x in sequence:
  54.         freq[x] = freq.get(x,0) + 1
  55.     return freq
  56.    
  57.  
  58. # (end of helper code)
  59. # -----------------------------------
  60.  
  61. #
  62. # Problem #1: Scoring a word
  63. #
  64. def get_word_score(word, n):
  65.     """
  66.    Returns the score for a word. Assumes the word is a
  67.    valid word.
  68.  
  69.     The score for a word is the sum of the points for letters
  70.     in the word multiplied by the length of the word, plus 50
  71.     points if all n letters are used on the first go.
  72.  
  73.     Letters are scored as in Scrabble; A is worth 1, B is
  74.     worth 3, C is worth 3, D is worth 2, E is worth 1, and so on.
  75.  
  76.    word: string (lowercase letters)
  77.    returns: int >= 0
  78.    """
  79.     score = 0
  80.     for c in word:
  81.         score = score + SCRABBLE_LETTER_VALUES[c]
  82.     score = score * len(word)
  83.     if len(word) == n:
  84.         score = score + 50
  85.     return score    
  86.    
  87. #
  88. # Make sure you understand how this function works and what it does!
  89. #
  90. def display_hand(hand):
  91.     """
  92.    Displays the letters currently in the hand.
  93.  
  94.    For example:
  95.       display_hand({'a':1, 'x':2, 'l':3, 'e':1})
  96.    Should print out something like:
  97.       a x x l l l e
  98.    The order of the letters is unimportant.
  99.  
  100.    hand: dictionary (string -> int)
  101.    """
  102.     for letter in hand.keys():
  103.         for j in range(hand[letter]):
  104.              print letter,              # print all on the same line
  105.     print                               # print an empty line
  106.  
  107. #
  108. # Make sure you understand how this function works and what it does!
  109. #
  110. def deal_hand(n):
  111.     """
  112.    Returns a random hand containing n lowercase letters.
  113.    At least n/3 the letters in the hand should be VOWELS.
  114.  
  115.    Hands are represented as dictionaries. The keys are
  116.    letters and the values are the number of times the
  117.    particular letter is repeated in that hand.
  118.  
  119.    n: int >= 0
  120.    returns: dictionary (string -> int)
  121.    """
  122.     hand={}
  123.     num_vowels = n / 3
  124.    
  125.     for i in range(num_vowels):
  126.         x = VOWELS[random.randrange(0,len(VOWELS))]
  127.         hand[x] = hand.get(x, 0) + 1
  128.        
  129.     for i in range(num_vowels, n):    
  130.         x = CONSONANTS[random.randrange(0,len(CONSONANTS))]
  131.         hand[x] = hand.get(x, 0) + 1
  132.        
  133.     return hand
  134.  
  135. #
  136. # Problem #2: Update a hand by removing letters
  137. #
  138. def update_hand(hand, word):
  139.     """
  140.    Assumes that 'hand' has all the letters in word.
  141.     In other words, this assumes that however many times
  142.     a letter appears in 'word', 'hand' has at least as
  143.     many of that letter in it.
  144.  
  145.    Updates the hand: uses up the letters in the given word
  146.    and returns the new hand, without those letters in it.
  147.  
  148.    Has no side effects: does not modify hand.
  149.  
  150.    word: string
  151.    hand: dictionary (string -> int)    
  152.    returns: dictionary (string -> int)
  153.    """
  154.     new_hand = hand
  155.     for c in word:
  156.         if new_hand[c] == 1:
  157.             del new_hand[c]
  158.         else:
  159.             new_hand[c] -= 1
  160.     return new_hand      
  161.        
  162.  
  163. #
  164. # Problem #3: Test word validity
  165. #
  166. def is_valid_word(word, hand, word_list):
  167.     """
  168.    Returns True if word is in the word_list and is entirely
  169.    composed of letters in the hand. Otherwise, returns False.
  170.    Does not mutate hand or word_list.
  171.    
  172.    word: string
  173.    hand: dictionary (string -> int)
  174.    word_list: list of lowercase strings
  175.    """
  176.     in_wordlist = word in word_list
  177.     in_hand = True
  178.    
  179.     for c in word:
  180.         in_hand = c in hand
  181.         if c in hand and hand[c] == 1:
  182.             del hand[c]
  183.         if c in hand and hand[c] > 1:
  184.             hand[c] = hand[c] - 1
  185.     #print 'hand ', hand    
  186.     if in_hand == True and in_wordlist == True:
  187.         return True
  188.     else:
  189.         return False
Advertisement
Add Comment
Please, Sign In to add comment