# 6.00 Problem Set 3 solution # # The Word Game I # VOWELS = 'aeiou' CONSONANTS = 'bcdfghjklmnpqrstvwxyz' HAND_SIZE = 10 # ----------------------------------- # Helper code # (you don't need to understand this helper code) from random import randrange import string WORDLIST_FILENAME = "words.txt" def load_words(): """ Returns a list of valid words. Words are strings of lowercase letters. Depending on the size of the word list, this function may take a while to finish. """ print "Loading word list from file..." # inFile: file inFile = open(WORDLIST_FILENAME, 'r', 0) # line: string line = inFile.readline() # wordlist: list of strings wordlist = string.split(line) print " ", len(wordlist), "words loaded." return wordlist def get_frequency_dict(sequence): """ Returns a dictionary where the keys are elements of the sequence and the values are integer counts, for the number of times that an element is repeated in the sequence. sequence: string or list return: dictionary """ # freqs: dictionary (element_type -> int) freq = {} for x in sequence: freq[x] = freq.get(x,0) + 1 return freq # (end of helper code) # ----------------------------------- # # Problem #1: Scoring a word # def get_word_score(word): """ Returns the score for a word. Assumes the word is a valid word. The score for a word is the sum of the scores of the individual letters, multiplied by the length of the word. Letters that are VOWELS get a score of 1. Letters that are CONSONANTS get a score of 2. word: string (lowercase letters) returns: int >= 0 """ score = 0 for ch in word: if ch in VOWELS: score += 1 else: score += 2 return score * len(word) # # Make sure you understand how this function works and what it does! # def display_hand(hand): """ Displays the letters currently in the hand. For example: display_hand({'a':1, 'x':2, 'l':3, 'e':1}) Should print out something like: a x x l l l e The order of the letters is unimportant. hand: dictionary (string -> int) """ for letter in hand.keys(): for j in range(hand[letter]): print letter, # print all on the same line print # print an empty line # # Make sure you understand how this function works and what it does! # def deal_hand(n): """ Returns a random hand containing n lowercase letters. At least n/3 the letters in the hand should be VOWELS. Hands are represented as dictionaries. The keys are letters and the values are the number of times the particular letter is repeated in that hand. n: int >= 0 returns: dictionary (string -> int) """ hand={} num_vowels = (n / 3 + 1) for i in range(num_vowels): x = VOWELS[randrange(0,len(VOWELS))] hand[x] = hand.get(x, 0) + 1 for i in range(num_vowels, n): x = CONSONANTS[randrange(0,len(CONSONANTS))] hand[x] = hand.get(x, 0) + 1 return hand # # Problem #2: Update a hand by removing letters # def update_hand(hand, word): """ Updates the hand: uses up the letters in the given word and returns the new hand, without those letters in it. Assumes that 'word' is a valid word. Therefore, all the letters in 'word' are in the given hand. word: string hand: dictionary (string -> int) returns: dictionary (string -> int) """ for ch in word: hand[ch] -= 1 return hand # # Problem #3: Test word validity # def is_valid_word(word, hand, word_list): """ Returns True if word is in the word_list and is entirely composed of letters in the hand. Otherwise, returns False. word: string hand: dictionary (string -> int) word_list: list of strings """ if word in word_list: freq = get_frequency_dict(word) for letter in freq: count = freq[letter] if count > hand.get(letter, 0): return False return True else: return False def hand_has_letters(hand): """ return true if the hand still has letters in it """ for letter in hand: if hand[letter] > 0: return True return False # # Problem #4: Playing a hand # def play_hand(hand, word_list): """ Allows the user to play the given hand, as follows: * The hand is displayed. * The user may input a word. * An invalid word is rejected, and a message is displayed asking the user to choose another word. * When a valid word is entered, it uses up letters from the hand. * After every valid word: the score for that word is displayed, the remaining letters in the hand are displayed, and the user is asked to input another word. * The sum of the word scores is displayed when the hand finishes. * The hand finishes when there are no more unused letters. The user can also finish playing the hand by inputing a single period (the string '.') instead of a word. hand: dictionary (string -> int) word_list: list of strings """ total = 0 while hand_has_letters(hand): print 'Current Hand: ', display_hand(hand) word = raw_input('Enter word, or a . to indicate that you are finished: ') if word == '.': break if is_valid_word(word, hand, word_list): update_hand(hand, word) score = get_word_score(word) total += score print word, 'earned', score, 'points. Total:', total, 'points' else: print 'Invalid word, please try again.' print 'Total score:', total, 'points.' # # Problem #5: Playing a game # Make sure you understand how this code works! # def play_game(word_list): """ Allow the user to play an arbitrary number of hands. * Asks the user to input 'n' or 'r' or 'e'. * If the user inputs 'n', let the user play a new (random) hand. When done playing the hand, ask the 'n' or 'e' question again. * If the user inputs 'r', let the user play the last hand again. * If the user inputs 'e', exit the game. * If the user inputs anything else, ask them again. """ hand = deal_hand(HAND_SIZE) # random init while True: cmd = raw_input('Enter n to deal a new hand, r to replay the last hand, or e to end game: ') if cmd == 'n': hand = deal_hand(HAND_SIZE) play_hand(hand.copy(), word_list) print elif cmd == 'r': play_hand(hand.copy(), word_list) print elif cmd == 'e': break else: print "Invalid command." # # Build data structures used for entire session and play game # if __name__ == '__main__': word_list = load_words() play_game(word_list)