Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

use python do this Problem 2. Dealing with hands **Please read problem 2 entirely before you begin coding the solution to problem 2** Representing hands

use python do this
Problem 2. Dealing with hands
**Please read problem 2 entirely before you begin coding the solution to problem 2**
Representing hands
A hand is the set of letters held by a player during the game. The player is initially dealt a set of random letters. For example, the player could start out with the following hand:
a, q, l, m, u, i, l
In our program, a hand will be represented as a dictionary: the keys are (lowercase) letters and the values are the number of times the particular letter is repeated in that hand. For example, the above hand would be represented as:
hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1}
Notice how the repeated letter 'l' is represented.
Notice that with a dictionary representation, the usual way to access a value is hand['a'], where 'a' is the key we want to find. However, this only works if the key is in the dictionary; otherwise, we get a KeyError. To avoid this, we can use the call hand.get('a',0). This is the safe way to access a value if we are not sure the key is in the dictionary. d.get(key,default) returns the value for key if key is in the dictionary d, else default. If default is not given, it returns None, so that this method never raises a KeyError.
Converting words into dictionary representation
One useful function weve defined for you is get_frequency_dict, defined near the top of ps3a.py. When given a string of letters as an input, it returns a dictionary where the keys are letters and the values are the number of times that letter is represented in the input string. For example:
>>> get_frequency_dict("hello")
{'h': 1, 'e': 1, 'l': 2, 'o': 1}
As you can see, this is the same kind of dictionary we use to represent hands.
Displaying a hand
Given a hand represented as a dictionary, we want to display it in a user-friendly way. We have provided the implementation for this in the display_hand function. Make sure you read through this carefully and understand what it does and how it works.
Generating a random hand
The hand a player is dealt is a set of letters chosen at random. We provide you with the implementation of a function that generates this random hand, deal_hand. The function takes as input a positive integer n, and returns a new object, hand containing n lowercase letters.
Removing letters from a hand (you implement this)
The player starts with a hand, a set of letters. As the player spells out words, letters from this set are used up. For example, the player could start out with the following hand:
a, q, l, m, u, i, l
The player could choose to spell the word quail. This would leave the following letters in the players hand:
l, m
You will now write a function that takes a hand and a word as inputs, uses letters from that hand to spell the word, and returns the remaining letters in the hand. For example:
>>> hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1}
>>> display_hand(hand)
a q l l m u i
>>> hand = update_hand(hand, 'quail')
>>> hand
{'l': 1, 'm': 1}
>>> display_hand(hand)
l m
(NOTE: alternatively, in the above example, after the call to update_hand the value of hand could be the dictionary {'a':0, 'q':0, 'l':1, 'm':1, 'u':0, 'i':0}. The exact value depends on your implementation; but the output of display_hand() should be the same in either case.)
Implement the update_hand function. Make sure this function has no side effects; i.e., it cannot mutate the hand passed in.
def update_hand(hand, word):
"""
Assumes that 'hand' has all the letters in word.
In other words, this assumes that however many times
a letter appears in 'word', 'hand' has at least as
many of that letter in it.
Updates the hand: uses up the letters in the given word
and returns the new hand, without those letters in it.
Has no side effects: does not modify hand.
word: string
hand: dictionary (string -> int)
returns: dictionary (string -> int)
"""
# TO DO ...
Testing: Make sure the test_update_hand() tests pass. You may also want to test your implementation of update_hand with some reasonable inputs.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions