Created
October 28, 2012 07:35
-
-
Save max-giro/3967977 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def displayHand(hand): | |
| """ | |
| Displays the letters currently in the hand. | |
| For example: | |
| >>> displayHand({'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 | |
| return "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def playHand(hand, wordList, n): | |
| """ | |
| Allows the user to play the given hand, as follows: | |
| * The hand is displayed. | |
| * The user may input a word or a single period (the string ".") | |
| to indicate they're done playing | |
| * Invalid words are rejected, and a message is displayed asking | |
| the user to choose another word until they enter a valid word or "." | |
| * 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 or the user | |
| inputs a "." | |
| hand: dictionary (string -> int) | |
| wordList: list of lowercase strings | |
| n: integer (HAND_SIZE; i.e., hand size required for additional points) | |
| """ | |
| # BEGIN PSEUDOCODE (download ps4a.py to see) | |
| number_letters_left_in_hand = n | |
| total_score = 0 | |
| # As long as there are still letters left in the hand: | |
| while(number_letters_left_in_hand > 0): | |
| # Display the hand | |
| print "Current Hand:", displayHand(hand) | |
| # Ask user for input | |
| word = raw_input('Enter word, or a "." to indicate that you are finished: ') | |
| # If the input is a single period: | |
| if(word == "."): | |
| # End the game (break out of the loop) | |
| print("Goodbye! Total score: " + str(total_score) + " points.") | |
| return | |
| # Otherwise (the input is not a single period): | |
| else: | |
| # If the word is not valid: | |
| if(not isValidWord(word, hand, wordList)): | |
| # Reject invalid word (print a message followed by a blank line) | |
| print("Invalid word, please try again.") | |
| print("") | |
| # Otherwise (the word is valid): | |
| else: | |
| # Tell the user how many points the word earned, and the updated total score, in one line followed by a blank line | |
| total_score = total_score + getWordScore(word, n) | |
| print("\"" + word + "\"" + " earned " + str(total_score) + " points. Total: " + str(total_score) + " points") | |
| print("") | |
| # Update the hand | |
| hand = updateHand(hand, word) | |
| number_letters_left_in_hand = calculateHandlen(hand) | |
| # Game is over (user entered a '.' or ran out of letters), so tell user the total score | |
| print("Run out of letters. Total score: " + str(total_score) + " points.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function call: playHand({'a': 2, 'p': 1, 'r': 1, 'e': 2, 't': 1}, '<edX internal wordList>', 7) | |
| Test 5: Full hand transcript - using all letters | |
| Your output: | |
| Current Hand: a a p r e e t | |
| None | |
| Enter word, or a "." to indicate that you are finished: pear | |
| "pear" earned 24 points. Total: 24 points | |
| Current Hand: a e t | |
| None | |
| Enter word, or a "." to indicate that you are finished: tea | |
| "tea" earned 33 points. Total: 33 points | |
| Run out of letters. Total score: 33 points. | |
| None | |
| Correct output: | |
| Current Hand: a a p r e e t | |
| Enter word, or a "." to indicate that you are finished: pear | |
| "pear" earned 24 points. Total: 24 points | |
| Current Hand: a e t | |
| Enter word, or a "." to indicate that you are finished: tea | |
| "tea" earned 9 points. Total: 33 points | |
| Run out of letters. Total score: 33 points. | |
| None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function call: playHand({'a': 2, 'p': 2, 'r': 1, 'z': 1, 'e': 1}, '<edX internal wordList>', 7) | |
| Test 6: Full hand transcript - can't use every letter | |
| Your output: | |
| Current Hand: a a p p r z e | |
| None | |
| Enter word, or a "." to indicate that you are finished: pear | |
| "pear" earned 24 points. Total: 24 points | |
| Current Hand: a p z | |
| None | |
| Enter word, or a "." to indicate that you are finished: za | |
| "za" earned 46 points. Total: 46 points | |
| Current Hand: p | |
| None | |
| Enter word, or a "." to indicate that you are finished: p | |
| Invalid word, please try again. | |
| Current Hand: p | |
| None | |
| Enter word, or a "." to indicate that you are finished: . | |
| Goodbye! Total score: 46 points. | |
| None | |
| Correct output: | |
| Current Hand: a a p p r z e | |
| Enter word, or a "." to indicate that you are finished: pear | |
| "pear" earned 24 points. Total: 24 points | |
| Current Hand: a p z | |
| Enter word, or a "." to indicate that you are finished: za | |
| "za" earned 22 points. Total: 46 points | |
| Current Hand: p | |
| Enter word, or a "." to indicate that you are finished: p | |
| Invalid word, please try again. | |
| Current Hand: p | |
| Enter word, or a "." to indicate that you are finished: . | |
| Goodbye! Total score: 46 points. | |
| None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment