0% found this document useful (0 votes)
50 views4 pages

Task 6

jhygtfr

Uploaded by

Meredit Manson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

Task 6

jhygtfr

Uploaded by

Meredit Manson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Dictionaries are another data structure that Python programmers can use to manage

larger amounts of data. While many of the exercises in this chapter can be solved with
lists or if statements, most (or even all) of them have solutions that are well suited to
dictionaries. As a result, you should use dictionaries to solve all these exercises instead
of (or in addition to) using the constructs that you have been introduced to in the
previous chapters. Completing the exercises in this chapter will help you learn to:
• Create a new variable that holds a dictionary
• Add a key-value pair to a dictionary
• Update the value associated with a key in a dictionary
• Iterate over all the keys and/or values in a dictionary
• Write functions that take dictionaries as parameters
Exercise128: Reverse Lookup
Write a function named reverse Lookup that finds all of the keys in a dictionary that
map to a specific value. The function will take the dictionary and the value to search for
as its only parameters. It will return a (possibly empty) list of keys from the dictionary
that map to the provided value. Include a main program that demonstrates the reverse
Lookup function as part of your solution to this exercise. Your program should create a
dictionary and then show that the reverse Lookup function works correctly when it
returns multiple keys, a single key, and no keys. Ensure that your main program only
runs when the file containing your solution to this exercise has not been imported into
another program.
Exercise129: Two Dice Simulation
In this exercise you will simulate 1,000 rolls of two dice. Begin by writing a function that
simulates rolling a pair of six-sided dice. Your function will not take any parameters. It
will return the total that was rolled on two dice as its only result. Write a main program
that uses your function to simulate rolling two six-sided dice 1,000 times. As your
program runs, it should count the number of times that each total occurs. Then it should
display a table that summarizes this data. Express the frequency for each total as a
percentage of the total number of rolls. Your program should also display the
percentage expected by probability theory for each total. Sample output is shown
below.
Exercise130: Text Messaging
On some basic cell phones, text messages can be sent using the numeric keypad.
Because each key has multiple letters associated with it, multiple key presses are
needed for most letters. Pressing the number once generates the first letter on the key.
Pressing the number 2, 3, 4 or 5 times generates the second, third, fourth or fifth
character listed for that key.

Write a program that displays the key presses that must be made to enter a text
message read from the user. Construct a dictionary that maps from each letter or
symbol to the key presses. Then use the dictionary to generate and display the presses
for the user’s message. For example, if the user enters Hello, World! then your program
should output 4433555555666110966677755531111. Ensure that your program
handles both uppercase and lowercase letters. Ignore any characters that aren’t listed in
the table above such as semicolons and brackets.
Exercise131:MorseCode
Morse code is an encoding scheme that uses dashes and dots to represent numbers and
letters. In this exercise, you will write a program that uses a dictionary to store
themappingfromlettersandnumberstoMorsecode.Useaperiodtorepresentadot, and a
hyphen to represent a dash. The mapping from letters and numbers to dashes and dots
is shown in Table 6.1. Your program should read a message from the user. Then it
should translate each letter and number in the message to Morse code, leaving a space
between each sequence of dashes and dots. Your program should ignore any characters
that are not letters or numbers. The Morse code for Hello, World! is shown below: .... . .-..
.-.. --- .-- --- .-. .-.. -..
Table6.1 Morse Code Letters and Numbers
A .- J .--- S ... 1 .---B -... K -.- T - 2 ..--C -.-. L .-.. U ..- 3 ...-D -.. M -- V ...- 4 ....E . N -. W .-- 5 .....
F ..-. O --- X -..- 6 -.... G --. P .--. Y -.-- 7 --... H .... Q --.- Z --.. 8 ---.. I .. R .-. 0 ----- 9 ----.
Morse code was originally developed in the nineteenth century for use over telegraph
wires. It is still used today, over 160 years after it was first created.
Exercise132: Postal Codes
In a Canadian postal code, the first, third and fifth characters are letters while the
second, fourth and sixth characters are numbers. The province can be determined from
the first character of a postal code, as shown in the following table. No valid postal codes
currently begin with D, F, I, O, Q, U, W, or Z.

The second character in a postal code identifies whether the address is rural or urban. If
that character is a 0 then the address is rural. Otherwise it is urban. Create a program
that reads a postal code from the user and displays the province associated with it,
along with whether the address is urban or rural. For example, if the user enters T2N
1N4 then your program should indicate that the postal code is for an urban address in
Alberta. If the user enters X0A 1B2 then your program should indicate that the postal
code is for a rural address in Nunavut or Northwest Territories. Use a dictionary to map
from the first character of the postal code to the province name. Display a meaningful
error message if the postal code begins with an invalid character.

Exercise133: Write Out Numbers in English


While the popularity of cheques as a payment method has diminished in recent years,
some companies still issue them to pay employees or vendors. The amount being paid
normally appears on a cheque twice, with one occurrence written using digits, and the
other occurrence written using English words. Repeating the amount in two different
forms makes it much more difficult for an unscrupulous employee or vendor to modify
the amount on the cheque before depositing it. In this exercise, your task is to create a
function that takes an integer between 0 and 999 as its only parameter, and returns a
string containing the English words for that number. For example, if the parameter to
the function is 142 then your function should return “one hundred forty two”. Use one
or more dictionaries to implement your solution rather than large if/elif/else
constructs. Include a main program that reads an integer from the user and displays its
value in English words.
Exercise134: Unique Characters
Create a program that determines and displays the number of unique characters in a
string entered by the user. For example, Hello, World! Has 10 unique characters while
zzz has only one unique character. Use a dictionary or set to solve this problem.
Exercise135: Anagrams
Two words are anagrams if they contain all of the same letters, but in a different order.
For example, “evil” and “live” are anagrams because each contains one e, one i, one l, and
one v. Create a program that reads two strings from the user, determines whether or
not they are anagrams, and reports the result.
Exercise136: Anagrams Again
The notion of anagrams can be extended to multiple words. For example, “William
Shakespeare” and “I am a weakish speller” are anagrams when capitalization and
spacing are ignored.
Extend your program from Exercise 135 so that it is able to check if two phrases are
anagrams. Your program should ignore capitalization, punctuation marks and spacing
when making the determination.

Exercise137: Scrabble™Score
In the game of Scrabble™, each letter has points associated with it. The total score of a
word is the sum of the scores of its letters. More common letters are worth fewer points
while less common letters are worth more points. The points associated with each letter
are shown below:

Write a program that computes and displays the Scrabble™ score for a word. Create a
dictionary that maps from letters to point values. Then use the dictionary to compute
the score.
A Scrabble™ board includes some squares that multiply the value of a letter or the value
of an entire word. We will ignore these squares in this exercise.

You might also like