SlideShare a Scribd company logo
1 of 21
PYTHON Homework HELP
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- support@pythonhomeworkhelp.com or
reach us at :- https://www.pythonhomeworkhelp.com/
Introduction
In this problem set, you'll implement a version of the 6.0001 word game!
Don't be intimidated by the length of this problem set. It's a lot of reading, but it is
very doable.
Let's begin by describing the word game: This game is a lot like Scrabble or Words With
Friends. Letters are dealt to players, who then construct one or more words using their
letters. Each valid word earns the user points, based on the length of the word and the
letters in that word.
The rules of the game are as follows. Do not start coding yet – as in Pset 2, we will
break this down into steps below!
Dealing
● A player is dealt a hand of HAND_SIZE letters of the alphabet, chosen at random.
This may include multiple instances of a particular letter.
● The player arranges the hand into as many words as they want out of the letters, but
using each letter at most once.
● Some letters may remain unused, though the size of the hand when a word is played
does affect its score.
Scoring
● The score for the hand is the sum of the score for each word formed.
● The score for a word is the product of two components:
Python Homework Help
o First component: the sum of the points for letters in the word.
o Second component: either [7 * word_length - 3 * (n-word_length)] or 1,
whichever value is greater, where:
▪ word_length is the number of letters used in the word
▪ n is the number of letters available in the current hand
● Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is worth 3, D is worth
2, E is worth 1, and so on. We have defined the dictionary SCRABBLE_LETTER_VALUES
that maps each lowercase letter to its Scrabble letter value.
● Examples:
o For example, if n=6 and the hand includes 1 'w', 2 'e's, and 1 'd' (as well as
two other letters), playing the word 'weed' would be worth 176 points:
(4+1+1+2) * (7*4 - 3*(6-4)) = 176. The first term is the sum of the values
of each letter used; the second term is the special computation that rewards
a player for playing a longer word, and penalizes them for any left over
letters.
Getting Started
1. Download and save ps3.zip. This includes the python file ps3.py, which
should contain all of your code, as it provides a set of initial procedures
and templates for new procedures. ps3.zip also includes a file for
testing your code test_ps3.py, and a file of legitimate words words.txt.
Do not change or delete anything in the file unless specified.
2. Run ps3.py, without making any modifications to it, in order to ensure
that everything is set up correctly. The code we have given you loads a
list of valid words from a file and then calls the play_game function. You
will implement the functions it needs in order to work. If everything is
okay, after a small delay, you should see the following printed out:
Python Homework Help
#
# Helper code
# (you don’t need to understand this helper code)
.
.
.
# (end of helper code)
#
Loading word list from file...
83667 words loaded.
play_game not yet
implemented.
If you see an IOError instead
(e.g., No such file or directory),
make sure you have saved
words.txt in the same directory
as ps3.py!
3. The file ps3.py has a number of
already-implemented functions you
can use while writing up your
solution. You can ignore the code
between the following comments,
though you should read and
understand everything else.
Python Homework Help
4. This problem set is structured so that you will write a number of modular
functions and then glue them together to form the complete game.
Instead of waiting until the entire game is ready, you should test each
function you write, individually, before moving on. This approach is
known as unit testing, and it will help you debug your code.
5. We have included some hints about how you might want to implement
some of the required functions in the included files. You don't need to
remove them in your final submission.
We have provided several test functions to get you started. As you make
progress on the problem set, run test_ps3.py to check your work so far.
If your code passes the unit tests you will see a SUCCESS message; otherwise you will see
a FAILURE message. These tests aren't exhaustive. You may want to test your code
in other ways too (for example, with different test values).
If you run test_ps3.py using the initially provided ps3.py skeleton, you should see that all
the tests fail.
These are the provided test functions:
test_get_word_score
Test the get_word_score implementation.
test_update_hand
Python Homework Help
Problem 1: Word scores
The first step is to implement a function that calculates the score for a
single word. Fill in the code for get_word_score in ps3.py according to
the function specifications.
As a reminder, here are the rules for scoring a word:
● The score for a word is the product of two components:
o First component: the sum of the points for letters in the word.
o Second component: either [7 * word_length - 3 * (n-
word_length)] or 1, whichever value is greater, where:
▪ word_length is the number of letters used in the word
▪ n is the number of letters available in the current hand
Test the update_hand
implementation.
test_is_valid_word
Test the is_valid_word
implementation.
test_wildcard
Test the modifications made to
support wildcards. (more about
those later on)
Python Homework Help
You should use the SCRABBLE_LETTER_VALUES dictionary defined at the top of
ps3.py. Do not assume that there are always 7 letters in a hand! The parameter
n is the total number of letters in the hand when the word was entered.
Finally, you may find the str.lower function helpful:
s = “My string”
print(s.lower())
>>>> “my string”
If you don’t know what this does you could try typing help(str.lower) in your
Spyder shell to see the documentation for the functions.
Testing: If this function is implemented correctly, and you run test_ps3.py, the
test_get_word_score()tests will pass. You should also test your implementation
of get_word_score yourself, using some reasonable English words. Note that the
wildcard tests will crash due to a KeyError. This is fine for now - you will fix this
in Problem 4.
Problem 2: Dealing with hands
Most of the functions described below have been implemented for you already.
Python Homework Help
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. 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 instead use
the function 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 it returns 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 we've defined for you is get_frequency_dict, defined near
the top of ps3.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:
Python Homework Help
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. Take a few minutes
right now to read through this function carefully and understand what it does and how it
works.
>> get_frequency_dict("hello")
{'h': 1, 'e': 1, 'l': 2, 'o': 1}
Generating a random hand
The hand a player is dealt is a set of letters chosen at random. We provide you
with a function that generates a random hand, deal_hand. The function takes as
input a positive integer n, and returns a new dictionary representing a hand of n
lowercase letters. Again, take a few minutes to read through this function
carefully and understand what it does and how it works.
Removing letters from a hand (you implement this!)
The player starts with a full hand of n letters. As the player spells out words,
letters from the set are used up. For example, the player could start with the
following hand: a, q, l, m, u, i, l The player could choose to play the word quail.
This would leave the following letters in the player's hand: l, m.
Python Homework Help
>> hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1}
>> display_hand(hand) a q l l m
u i
>> new_hand = update_hand(hand, 'quail')
>> new_hand
{'l': 1, 'm': 1}
>> display_hand(new_hand) l m
>> display_hand(hand) a q l l m
u i
(NOTE: Alternatively, in the above example, after the call to update_hand the value of
new_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.)
IMPORTANT: If the player guesses a word that is invalid, either because it is not a real
word or because they used letters that they don't actually have in their hand, they still lose
the letters from their hand that they did guess as a penalty. Make sure that your
implementation accounts for this! Do not assume that the word you are given only uses
letters that actually exist in the hand. For example:
>> hand = {'j':2, 'o':1, 'l':1, 'w':1, 'n':2}
>> display_hand(hand) j j o l w
n n
>> hand = update_hand(hand, 'jolly')
>> hand
{'j':1, w':1, 'n':2}
>> display_hand(hand) j w n n
Python Homework Help
Note that one 'j', one 'o', and one 'l' (despite that facts that the player tried
to use two, because only one existed in the hand) were used up. The 'y' guess has no
effect on the hand, because 'y' was not in the hand to begin with. Also, the same note from
above about alternate representations of the hand applies here.
Implement the update_hand function according to the specifications in the skeleton code.
HINT: You may wish to review the documentation for the ".copy" method of Python
dictionaries.
Testing: Make sure the test_update_hand tests pass. You may also want to test your
implementation of update_hand with some reasonable
inputs.
Problem 3. Valid words
At this point, we have not written any code to verify that a word given by a player obeys
the rules of the game. A valid word is in the word list (we ignore the case of words here)
and it is composed entirely of letters from the current hand.
Implement the is_valid_word function according to its specifications.
implementation with some reasonable inputs. In particular, you may want to test your
behavior be?
Testing: Make sure the test_is_valid_word tests pass. You should also test your
implementation by calling it multiple times on the same hand - what should the correct
Python Homework Help
Problem 4. Wildcards
We want to allow hands to contain wildcard letters, which will be denoted by an asterisk
(*). Wildcards can only replace vowels. Each hand dealt should initially contain exactly
one wildcard as one of its letters. The player does not receive any points for using the
wildcard (unlike all the other letters), though it does count as a used or unused letter
when scoring.
During the game, a player wishing to use a wildcard should enter "*" (without quotes)
instead of the intended letter. The word-validation code should not make any assumptions
about what the intended vowel should be, but should verify that at least one valid word
can be made with the wildcard as a vowel in the desired position.
The examples below show how wildcards should behave in the context of playing a
hand, which you will implement in Problem 5 below. Don’t worry about that part yet -
just pay attention to how the wildcard is handled.
Example #1: A valid word made without the wildcard
Current Hand: c o w s * z
Enter word, or "!!" to indicate that you are finished: cows
"cows" earned 198 points. Total: 198 points
Current Hand: * z
Enter word, or "!!" to indicate that you are finished: !!
Total score: 198 points
Example #2: A valid word made using the wildcard
Current Hand: c o w s * z
Enter word, or "!!" to indicate that you are finished: c*ws
"c*ws" earned 176 points. Total: 176 points Python Homework Help
Example #3: An invalid word with a wildcard
Current Hand: c o w s * z
Enter word, or "!!" to indicate that you are
finished: c*wz That is not a valid word. Please
choose another word.
Current Hand: o s
Enter word, or "!!" to indicate that you are
finished: !! Total score: 0 points
Example #4: Another invalid word with a wildcard
Current Hand: c o w s * z
Enter word, or "!!" to indicate that you are
finished: *ows That is not a valid word. Please
choose another word.
Current Hand: c z
Enter word, or "!!" to indicate that you are
finished: !! Total score: 0 points
Modify the deal_hand function to support always giving one wildcard in each hand. Note
that deal_hand currently ensures that one third of the letters are vowels and the rest are
consonants. Leave the consonant count intact, and replace one of the vowel slots with the
wildcard. You will also need to modify one or more of the constants defined at the top of the
file to account for wildcards.
Python Homework Help
Then modify the is_valid_word function to support wildcards.
Hint: Check to see what
possible words can be formed by replacing the wildcard with other vowels.
review the documentation for string module’s find() function and make note of its
behavior when a character is not found. The constant VOWELS defined for you at the top of
the file may be helpful as well.
Testing: Make sure the test_wildcard tests pass. You may also want to test your
implementation with some reasonable inputs.
Problem 5. Playing a hand
We are now ready to begin writing the code that interacts with the player.
Implement the play_hand function. This function allows the user to play out a single hand.
You'll first need to implement the helper function calculate_handlen, which can be done in
under five lines of code.
Note that after the line # BEGIN PSEUDOCODE there is a bunch of, well,
pseudocode! This is to help guide you in writing your function. Check out
the Why Pseudocode? resource to learn more about the What and Why
of Pseudocode before you start this problem.
Testing: Try out your implementation as if you were playing the game: run your
program and call the play_hand function from your shell with a hand and the word_list.
Note: Your output should match the examples below. You should not print
extraneous "None" messages.
Python Homework Help
Example #1
Current Hand: a j e f * r x
Enter word, or "!!" to indicate that you are finished: jar
"jar" earned 90 points. Total: 90 points
Current Hand: * f x e
Enter word, or "!!" to indicate that you are finished: f*x
"f*x" earned 216 points. Total: 306 points
Current Hand: e
Enter word, or "!!" to indicate that you are finished: !!
Total score: 306 points
Example #2
Current Hand: a c f i * t x
Enter word, or "!!" to indicate that you are finished: fix
"fix" earned 117 points. Total: 117 points
Current Hand: a c t *
Enter word, or "!!" to indicate that you are finished: ac That
is not a valid word. Please choose another word.
Current Hand: t *
Enter word, or "!!" to indicate that you are finished: *t "*t"
earned 14 points. Total: 131 points
Ran out of letters. Total score: 131 points
Python Homework Help
Problem 6. Playing a game
A game consists of playing multiple hands. We need to implement two final functions to
complete our wordgame.
Do not assume that there will always be 7 letters in a hand! Our goal is to keep the code
modular - if you want to try playing your word game with 10 letters or 4 letters you will be
able to do it by simply changing the value of HAND_SIZE!
When implementing substitution, you might want to check the methods associated with
dictionaries, such as .keys, or review the del keyword. You may also want to look at the
code for deal_hand to see how random.choice can be used to select an element at random
from a set of elements (such as a string).
Note that we are not providing you with pseudocode for this problem. However, as you are
deciding how to implement these functions, you may want to write your own as a guideline.
Testing: Try out this implementation as if you were playing the game. Try out different
values for HAND_SIZE with your program, and be sure that you can play the word game with
different hand sizes by modifying only the variable HAND_SIZE.
Example
Enter total
number of
hands: 2
Current hand: a
c i * p r t
Would you like to substitute a
letter? no
Python Homework Help
Current hand: a c i * p r t
Please enter a word or '!!' to indicate you are
done: part "part" earned 114 points. Total:
114 points
Current hand: c i *
Please enter a word or '!!' to indicate you
are done: ic* "ic*" earned 84 points. Total:
198 points
Ran out of letters
Total score for this hand: 198
Would you like to replay the
hand? no Current hand: d d *
l o u t
Would you like to substitute a
letter? yes Which letter would you
like to replace: l
Current hand: d d * a o u t
Please enter a word or '!!' to indicate you
are done: out "out" earned 27 points. Total:
27 points
Current hand: d d * a
Please enter a word or '!!' to indicate you
are done: !! Total score for this hand: 27
Python Homework Help
Would you like to
replay the hand? yes
Current hand: d d * a
o u t
Please enter a word or '!!' to
indicate you are done: d*d "d*d"
earned 36 points. Total: 36 points
Current hand: a o u t
Please enter a word or '!!' to
indicate you are done: out
from ps3 import *
#
# Test code
#
def test_get_word_score():
"""
Unit test for get_word_score
"""
failure=False
# dictionary of words and scores
words = {("", 7):0, ("it", 7):2, ("was", 7):54,
("weed", 6):176,
("scored", 7):351, ("WaYbILl", 7):735,
Python Homework Help
("Outgnaw", 7):539,
("fork", 7):209, ("FORK",
4):308}
for (word, n) in words.keys():
score = get_word_score(word, n)
if score != words[(word, n)]:
print("FAILURE:
test_get_word_score()")
print("tExpected",
words[(word, n)], "points but got '" +

str(score) + "' for
word '" + word + "', n=" + str(n))
failure=True
if not failure:
print("SUCCESS:
test_get_word_score()")
# end of test_get_word_score
def test_update_hand():
"""
Unit test for update_hand
"""
# test 1
handOrig = {'a':1, 'q':1,
'l':2, 'm':1, 'u':1, 'i':1}
Python Homework Help
hand2 = update_hand(handCopy, word)
expected_hand1 = {'l':1, 'm':1}
expected_hand2 = {'a':0, 'q':0, 'l':1, 'm':1, 'u':0, 'i':0}
if hand2 != expected_hand1 and hand2 != expected_hand2:
print("FAILURE: test_update_hand('"+ word +"', " +
str(handOrig) + ")")
print("tReturned: ", hand2, "nt-- but expected:",
expected_hand1, "or", expected_hand2)
return # exit function
if handCopy != handOrig:
print("FAILURE: test_update_hand('"+ word +"', " +
str(handOrig) + ")")
print("tOriginal hand was", handOrig)
print("tbut implementation of update_hand mutated the
original hand!")
print("tNow the hand looks like this:", handCopy)
handCopy = handOrig.copy()
word = "quail"
return # exit function
# test 2
Python Homework Help
handOrig = {'e':1, 'v':2, 'n':1, 'i':1, 'l':2}
handCopy = handOrig.copy()
word = "Evil"
hand2 = update_hand(handCopy, word)
expected_hand1 = {'v':1, 'n':1, 'l':1}
expected_hand2 = {'e':0, 'v':1, 'n':1, 'i':0, 'l':1}
if hand2 != expected_hand1 and hand2 != expected_hand2:
print("FAILURE: test_update_hand('"+ word +"', " +
str(handOrig) + ")")
print("tReturned: ", hand2, "nt-- but expected:",
expected_hand1, "or", expected_hand2)
return # exit function
if handCopy != handOrig:
print("FAILURE: test_update_hand('"+ word +"', " +
str(handOrig) + ")")
print("tOriginal hand was", handOrig)
print("tbut implementation of update_hand mutated the
original hand!")
print("tNow the hand looks like this:", handCopy)
return # exit function
Python Homework Help

More Related Content

What's hot

Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review GuideBenjamin Kissinger
 
Introduction to Python Basics
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python BasicsRaghunath A
 
Introduction to Boost regex
Introduction to Boost regexIntroduction to Boost regex
Introduction to Boost regexYongqiang Li
 
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)Akhil Mittal
 
Souvenir's Booth - Algorithm Design and Analysis Project Project Report
Souvenir's Booth - Algorithm Design and Analysis Project Project ReportSouvenir's Booth - Algorithm Design and Analysis Project Project Report
Souvenir's Booth - Algorithm Design and Analysis Project Project ReportAkshit Arora
 
Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsAbed Bukhari
 
Pointers andmemory
Pointers andmemoryPointers andmemory
Pointers andmemoryariffast
 
사람들과 자연스러운 대화를 나누는 일상대화 인공지능 만들기
사람들과 자연스러운 대화를 나누는 일상대화 인공지능 만들기사람들과 자연스러운 대화를 나누는 일상대화 인공지능 만들기
사람들과 자연스러운 대화를 나누는 일상대화 인공지능 만들기NAVER Engineering
 
Natural Language Processing made easy
Natural Language Processing made easyNatural Language Processing made easy
Natural Language Processing made easyGopi Krishnan Nambiar
 
Strings in Python
Strings in PythonStrings in Python
Strings in Pythonnitamhaske
 
Parts of Speect Tagging
Parts of Speect TaggingParts of Speect Tagging
Parts of Speect Taggingtheyaseen51
 
Strings Objects Variables
Strings Objects VariablesStrings Objects Variables
Strings Objects VariablesChaffey College
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python ProgrammingKamal Acharya
 

What's hot (19)

Python Programming Homework Help
Python Programming Homework HelpPython Programming Homework Help
Python Programming Homework Help
 
Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review Guide
 
Python course
Python coursePython course
Python course
 
Introduction to Python Basics
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python Basics
 
Introduction to Boost regex
Introduction to Boost regexIntroduction to Boost regex
Introduction to Boost regex
 
Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
 
Souvenir's Booth - Algorithm Design and Analysis Project Project Report
Souvenir's Booth - Algorithm Design and Analysis Project Project ReportSouvenir's Booth - Algorithm Design and Analysis Project Project Report
Souvenir's Booth - Algorithm Design and Analysis Project Project Report
 
Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressions
 
Pointers andmemory
Pointers andmemoryPointers andmemory
Pointers andmemory
 
사람들과 자연스러운 대화를 나누는 일상대화 인공지능 만들기
사람들과 자연스러운 대화를 나누는 일상대화 인공지능 만들기사람들과 자연스러운 대화를 나누는 일상대화 인공지능 만들기
사람들과 자연스러운 대화를 나누는 일상대화 인공지능 만들기
 
Natural Language Processing made easy
Natural Language Processing made easyNatural Language Processing made easy
Natural Language Processing made easy
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
R P G Generator
R P G  GeneratorR P G  Generator
R P G Generator
 
 
Parts of Speect Tagging
Parts of Speect TaggingParts of Speect Tagging
Parts of Speect Tagging
 
Strings Objects Variables
Strings Objects VariablesStrings Objects Variables
Strings Objects Variables
 
PHP
 PHP PHP
PHP
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
 

Similar to Python Homework Help

Similar to Python Homework Help (20)

python.pdf
python.pdfpython.pdf
python.pdf
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Ppt. By Me.pptx
Ppt. By Me.pptxPpt. By Me.pptx
Ppt. By Me.pptx
 
1 ECE 175 Computer Programming for Engineering Applica.docx
1  ECE 175 Computer Programming for Engineering Applica.docx1  ECE 175 Computer Programming for Engineering Applica.docx
1 ECE 175 Computer Programming for Engineering Applica.docx
 
ForLoops.pptx
ForLoops.pptxForLoops.pptx
ForLoops.pptx
 
Feature Engineering in NLP.pdf
Feature Engineering in NLP.pdfFeature Engineering in NLP.pdf
Feature Engineering in NLP.pdf
 
Sam python pro_points_slide
Sam python pro_points_slideSam python pro_points_slide
Sam python pro_points_slide
 
gdscpython.pdf
gdscpython.pdfgdscpython.pdf
gdscpython.pdf
 
Python breakdown-workbook
Python breakdown-workbookPython breakdown-workbook
Python breakdown-workbook
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
 
Learn python
Learn pythonLearn python
Learn python
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
Hey i have attached the required file for my assignment.and addi
Hey i have attached the required file for my assignment.and addiHey i have attached the required file for my assignment.and addi
Hey i have attached the required file for my assignment.and addi
 

More from Python Homework Help

Introduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxIntroduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Python Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Homework Help
 

More from Python Homework Help (20)

Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Complete my Python Homework
Complete my Python HomeworkComplete my Python Homework
Complete my Python Homework
 
Introduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxIntroduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptx
 
Basic Python Programming.pptx
Basic Python Programming.pptxBasic Python Programming.pptx
Basic Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Programming Homework Help.pptx
Python Programming Homework Help.pptx
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
 
Perfect Python Homework Help
Perfect Python Homework HelpPerfect Python Homework Help
Perfect Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 

Recently uploaded

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 

Python Homework Help

  • 1. PYTHON Homework HELP For any Homework related queries, Call us at : - +1 678 648 4277 You can mail us at :- support@pythonhomeworkhelp.com or reach us at :- https://www.pythonhomeworkhelp.com/
  • 2. Introduction In this problem set, you'll implement a version of the 6.0001 word game! Don't be intimidated by the length of this problem set. It's a lot of reading, but it is very doable. Let's begin by describing the word game: This game is a lot like Scrabble or Words With Friends. Letters are dealt to players, who then construct one or more words using their letters. Each valid word earns the user points, based on the length of the word and the letters in that word. The rules of the game are as follows. Do not start coding yet – as in Pset 2, we will break this down into steps below! Dealing ● A player is dealt a hand of HAND_SIZE letters of the alphabet, chosen at random. This may include multiple instances of a particular letter. ● The player arranges the hand into as many words as they want out of the letters, but using each letter at most once. ● Some letters may remain unused, though the size of the hand when a word is played does affect its score. Scoring ● The score for the hand is the sum of the score for each word formed. ● The score for a word is the product of two components: Python Homework Help
  • 3. o First component: the sum of the points for letters in the word. o Second component: either [7 * word_length - 3 * (n-word_length)] or 1, whichever value is greater, where: ▪ word_length is the number of letters used in the word ▪ n is the number of letters available in the current hand ● Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is worth 3, D is worth 2, E is worth 1, and so on. We have defined the dictionary SCRABBLE_LETTER_VALUES that maps each lowercase letter to its Scrabble letter value. ● Examples: o For example, if n=6 and the hand includes 1 'w', 2 'e's, and 1 'd' (as well as two other letters), playing the word 'weed' would be worth 176 points: (4+1+1+2) * (7*4 - 3*(6-4)) = 176. The first term is the sum of the values of each letter used; the second term is the special computation that rewards a player for playing a longer word, and penalizes them for any left over letters. Getting Started 1. Download and save ps3.zip. This includes the python file ps3.py, which should contain all of your code, as it provides a set of initial procedures and templates for new procedures. ps3.zip also includes a file for testing your code test_ps3.py, and a file of legitimate words words.txt. Do not change or delete anything in the file unless specified. 2. Run ps3.py, without making any modifications to it, in order to ensure that everything is set up correctly. The code we have given you loads a list of valid words from a file and then calls the play_game function. You will implement the functions it needs in order to work. If everything is okay, after a small delay, you should see the following printed out: Python Homework Help
  • 4. # # Helper code # (you don’t need to understand this helper code) . . . # (end of helper code) # Loading word list from file... 83667 words loaded. play_game not yet implemented. If you see an IOError instead (e.g., No such file or directory), make sure you have saved words.txt in the same directory as ps3.py! 3. The file ps3.py has a number of already-implemented functions you can use while writing up your solution. You can ignore the code between the following comments, though you should read and understand everything else. Python Homework Help
  • 5. 4. This problem set is structured so that you will write a number of modular functions and then glue them together to form the complete game. Instead of waiting until the entire game is ready, you should test each function you write, individually, before moving on. This approach is known as unit testing, and it will help you debug your code. 5. We have included some hints about how you might want to implement some of the required functions in the included files. You don't need to remove them in your final submission. We have provided several test functions to get you started. As you make progress on the problem set, run test_ps3.py to check your work so far. If your code passes the unit tests you will see a SUCCESS message; otherwise you will see a FAILURE message. These tests aren't exhaustive. You may want to test your code in other ways too (for example, with different test values). If you run test_ps3.py using the initially provided ps3.py skeleton, you should see that all the tests fail. These are the provided test functions: test_get_word_score Test the get_word_score implementation. test_update_hand Python Homework Help
  • 6. Problem 1: Word scores The first step is to implement a function that calculates the score for a single word. Fill in the code for get_word_score in ps3.py according to the function specifications. As a reminder, here are the rules for scoring a word: ● The score for a word is the product of two components: o First component: the sum of the points for letters in the word. o Second component: either [7 * word_length - 3 * (n- word_length)] or 1, whichever value is greater, where: ▪ word_length is the number of letters used in the word ▪ n is the number of letters available in the current hand Test the update_hand implementation. test_is_valid_word Test the is_valid_word implementation. test_wildcard Test the modifications made to support wildcards. (more about those later on) Python Homework Help
  • 7. You should use the SCRABBLE_LETTER_VALUES dictionary defined at the top of ps3.py. Do not assume that there are always 7 letters in a hand! The parameter n is the total number of letters in the hand when the word was entered. Finally, you may find the str.lower function helpful: s = “My string” print(s.lower()) >>>> “my string” If you don’t know what this does you could try typing help(str.lower) in your Spyder shell to see the documentation for the functions. Testing: If this function is implemented correctly, and you run test_ps3.py, the test_get_word_score()tests will pass. You should also test your implementation of get_word_score yourself, using some reasonable English words. Note that the wildcard tests will crash due to a KeyError. This is fine for now - you will fix this in Problem 4. Problem 2: Dealing with hands Most of the functions described below have been implemented for you already. Python Homework Help
  • 8. 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. 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 instead use the function 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 it returns 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 we've defined for you is get_frequency_dict, defined near the top of ps3.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: Python Homework Help
  • 9. 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. Take a few minutes right now to read through this function carefully and understand what it does and how it works. >> get_frequency_dict("hello") {'h': 1, 'e': 1, 'l': 2, 'o': 1} Generating a random hand The hand a player is dealt is a set of letters chosen at random. We provide you with a function that generates a random hand, deal_hand. The function takes as input a positive integer n, and returns a new dictionary representing a hand of n lowercase letters. Again, take a few minutes to read through this function carefully and understand what it does and how it works. Removing letters from a hand (you implement this!) The player starts with a full hand of n letters. As the player spells out words, letters from the set are used up. For example, the player could start with the following hand: a, q, l, m, u, i, l The player could choose to play the word quail. This would leave the following letters in the player's hand: l, m. Python Homework Help
  • 10. >> hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1} >> display_hand(hand) a q l l m u i >> new_hand = update_hand(hand, 'quail') >> new_hand {'l': 1, 'm': 1} >> display_hand(new_hand) l m >> display_hand(hand) a q l l m u i (NOTE: Alternatively, in the above example, after the call to update_hand the value of new_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.) IMPORTANT: If the player guesses a word that is invalid, either because it is not a real word or because they used letters that they don't actually have in their hand, they still lose the letters from their hand that they did guess as a penalty. Make sure that your implementation accounts for this! Do not assume that the word you are given only uses letters that actually exist in the hand. For example: >> hand = {'j':2, 'o':1, 'l':1, 'w':1, 'n':2} >> display_hand(hand) j j o l w n n >> hand = update_hand(hand, 'jolly') >> hand {'j':1, w':1, 'n':2} >> display_hand(hand) j w n n Python Homework Help
  • 11. Note that one 'j', one 'o', and one 'l' (despite that facts that the player tried to use two, because only one existed in the hand) were used up. The 'y' guess has no effect on the hand, because 'y' was not in the hand to begin with. Also, the same note from above about alternate representations of the hand applies here. Implement the update_hand function according to the specifications in the skeleton code. HINT: You may wish to review the documentation for the ".copy" method of Python dictionaries. Testing: Make sure the test_update_hand tests pass. You may also want to test your implementation of update_hand with some reasonable inputs. Problem 3. Valid words At this point, we have not written any code to verify that a word given by a player obeys the rules of the game. A valid word is in the word list (we ignore the case of words here) and it is composed entirely of letters from the current hand. Implement the is_valid_word function according to its specifications. implementation with some reasonable inputs. In particular, you may want to test your behavior be? Testing: Make sure the test_is_valid_word tests pass. You should also test your implementation by calling it multiple times on the same hand - what should the correct Python Homework Help
  • 12. Problem 4. Wildcards We want to allow hands to contain wildcard letters, which will be denoted by an asterisk (*). Wildcards can only replace vowels. Each hand dealt should initially contain exactly one wildcard as one of its letters. The player does not receive any points for using the wildcard (unlike all the other letters), though it does count as a used or unused letter when scoring. During the game, a player wishing to use a wildcard should enter "*" (without quotes) instead of the intended letter. The word-validation code should not make any assumptions about what the intended vowel should be, but should verify that at least one valid word can be made with the wildcard as a vowel in the desired position. The examples below show how wildcards should behave in the context of playing a hand, which you will implement in Problem 5 below. Don’t worry about that part yet - just pay attention to how the wildcard is handled. Example #1: A valid word made without the wildcard Current Hand: c o w s * z Enter word, or "!!" to indicate that you are finished: cows "cows" earned 198 points. Total: 198 points Current Hand: * z Enter word, or "!!" to indicate that you are finished: !! Total score: 198 points Example #2: A valid word made using the wildcard Current Hand: c o w s * z Enter word, or "!!" to indicate that you are finished: c*ws "c*ws" earned 176 points. Total: 176 points Python Homework Help
  • 13. Example #3: An invalid word with a wildcard Current Hand: c o w s * z Enter word, or "!!" to indicate that you are finished: c*wz That is not a valid word. Please choose another word. Current Hand: o s Enter word, or "!!" to indicate that you are finished: !! Total score: 0 points Example #4: Another invalid word with a wildcard Current Hand: c o w s * z Enter word, or "!!" to indicate that you are finished: *ows That is not a valid word. Please choose another word. Current Hand: c z Enter word, or "!!" to indicate that you are finished: !! Total score: 0 points Modify the deal_hand function to support always giving one wildcard in each hand. Note that deal_hand currently ensures that one third of the letters are vowels and the rest are consonants. Leave the consonant count intact, and replace one of the vowel slots with the wildcard. You will also need to modify one or more of the constants defined at the top of the file to account for wildcards. Python Homework Help
  • 14. Then modify the is_valid_word function to support wildcards. Hint: Check to see what possible words can be formed by replacing the wildcard with other vowels. review the documentation for string module’s find() function and make note of its behavior when a character is not found. The constant VOWELS defined for you at the top of the file may be helpful as well. Testing: Make sure the test_wildcard tests pass. You may also want to test your implementation with some reasonable inputs. Problem 5. Playing a hand We are now ready to begin writing the code that interacts with the player. Implement the play_hand function. This function allows the user to play out a single hand. You'll first need to implement the helper function calculate_handlen, which can be done in under five lines of code. Note that after the line # BEGIN PSEUDOCODE there is a bunch of, well, pseudocode! This is to help guide you in writing your function. Check out the Why Pseudocode? resource to learn more about the What and Why of Pseudocode before you start this problem. Testing: Try out your implementation as if you were playing the game: run your program and call the play_hand function from your shell with a hand and the word_list. Note: Your output should match the examples below. You should not print extraneous "None" messages. Python Homework Help
  • 15. Example #1 Current Hand: a j e f * r x Enter word, or "!!" to indicate that you are finished: jar "jar" earned 90 points. Total: 90 points Current Hand: * f x e Enter word, or "!!" to indicate that you are finished: f*x "f*x" earned 216 points. Total: 306 points Current Hand: e Enter word, or "!!" to indicate that you are finished: !! Total score: 306 points Example #2 Current Hand: a c f i * t x Enter word, or "!!" to indicate that you are finished: fix "fix" earned 117 points. Total: 117 points Current Hand: a c t * Enter word, or "!!" to indicate that you are finished: ac That is not a valid word. Please choose another word. Current Hand: t * Enter word, or "!!" to indicate that you are finished: *t "*t" earned 14 points. Total: 131 points Ran out of letters. Total score: 131 points Python Homework Help
  • 16. Problem 6. Playing a game A game consists of playing multiple hands. We need to implement two final functions to complete our wordgame. Do not assume that there will always be 7 letters in a hand! Our goal is to keep the code modular - if you want to try playing your word game with 10 letters or 4 letters you will be able to do it by simply changing the value of HAND_SIZE! When implementing substitution, you might want to check the methods associated with dictionaries, such as .keys, or review the del keyword. You may also want to look at the code for deal_hand to see how random.choice can be used to select an element at random from a set of elements (such as a string). Note that we are not providing you with pseudocode for this problem. However, as you are deciding how to implement these functions, you may want to write your own as a guideline. Testing: Try out this implementation as if you were playing the game. Try out different values for HAND_SIZE with your program, and be sure that you can play the word game with different hand sizes by modifying only the variable HAND_SIZE. Example Enter total number of hands: 2 Current hand: a c i * p r t Would you like to substitute a letter? no Python Homework Help
  • 17. Current hand: a c i * p r t Please enter a word or '!!' to indicate you are done: part "part" earned 114 points. Total: 114 points Current hand: c i * Please enter a word or '!!' to indicate you are done: ic* "ic*" earned 84 points. Total: 198 points Ran out of letters Total score for this hand: 198 Would you like to replay the hand? no Current hand: d d * l o u t Would you like to substitute a letter? yes Which letter would you like to replace: l Current hand: d d * a o u t Please enter a word or '!!' to indicate you are done: out "out" earned 27 points. Total: 27 points Current hand: d d * a Please enter a word or '!!' to indicate you are done: !! Total score for this hand: 27 Python Homework Help
  • 18. Would you like to replay the hand? yes Current hand: d d * a o u t Please enter a word or '!!' to indicate you are done: d*d "d*d" earned 36 points. Total: 36 points Current hand: a o u t Please enter a word or '!!' to indicate you are done: out from ps3 import * # # Test code # def test_get_word_score(): """ Unit test for get_word_score """ failure=False # dictionary of words and scores words = {("", 7):0, ("it", 7):2, ("was", 7):54, ("weed", 6):176, ("scored", 7):351, ("WaYbILl", 7):735, Python Homework Help
  • 19. ("Outgnaw", 7):539, ("fork", 7):209, ("FORK", 4):308} for (word, n) in words.keys(): score = get_word_score(word, n) if score != words[(word, n)]: print("FAILURE: test_get_word_score()") print("tExpected", words[(word, n)], "points but got '" + str(score) + "' for word '" + word + "', n=" + str(n)) failure=True if not failure: print("SUCCESS: test_get_word_score()") # end of test_get_word_score def test_update_hand(): """ Unit test for update_hand """ # test 1 handOrig = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1} Python Homework Help
  • 20. hand2 = update_hand(handCopy, word) expected_hand1 = {'l':1, 'm':1} expected_hand2 = {'a':0, 'q':0, 'l':1, 'm':1, 'u':0, 'i':0} if hand2 != expected_hand1 and hand2 != expected_hand2: print("FAILURE: test_update_hand('"+ word +"', " + str(handOrig) + ")") print("tReturned: ", hand2, "nt-- but expected:", expected_hand1, "or", expected_hand2) return # exit function if handCopy != handOrig: print("FAILURE: test_update_hand('"+ word +"', " + str(handOrig) + ")") print("tOriginal hand was", handOrig) print("tbut implementation of update_hand mutated the original hand!") print("tNow the hand looks like this:", handCopy) handCopy = handOrig.copy() word = "quail" return # exit function # test 2 Python Homework Help
  • 21. handOrig = {'e':1, 'v':2, 'n':1, 'i':1, 'l':2} handCopy = handOrig.copy() word = "Evil" hand2 = update_hand(handCopy, word) expected_hand1 = {'v':1, 'n':1, 'l':1} expected_hand2 = {'e':0, 'v':1, 'n':1, 'i':0, 'l':1} if hand2 != expected_hand1 and hand2 != expected_hand2: print("FAILURE: test_update_hand('"+ word +"', " + str(handOrig) + ")") print("tReturned: ", hand2, "nt-- but expected:", expected_hand1, "or", expected_hand2) return # exit function if handCopy != handOrig: print("FAILURE: test_update_hand('"+ word +"', " + str(handOrig) + ")") print("tOriginal hand was", handOrig) print("tbut implementation of update_hand mutated the original hand!") print("tNow the hand looks like this:", handCopy) return # exit function Python Homework Help