To start this assignment, download this zip file.
Homework 5a — Dictionaries
1. Postal routing
Write a program in postal_routing.py
that sorts mail into bins. This program
takes two arguments on the command line:
- an input file of addresses
- an output file
An example of the input file is in addresses.txt
:
9927 Giffin Ct, Windermere, FL 34786
221B Baker St., London 5208
350 Fifth Avenue, New York City, NY 10118
62 West Wallaby Street, Wigan, Lancashire 227
17 Cherry Tree Lane, London 5208
You are given a dictionary that maps zip codes to bins:
zip_code_to_delivery_bin = {
5208: 16,
10118: 4,
227: 76,
12345: 1,
84604: 25,
84602: 25,
20895: 82
}
Your program should go through each address in the input file, one by one, find the zip code, and then map that zip code to its bin. The program should write the bins, one per line to the output file. If no bin is found the program should write “unknown” for that line.
Given the above dictionary, if you run the program:
python postal_routing.py addresses.txt bins.txt
then the file bins.txt
should contain:
unknown
16
4
76
16
We have given you starter code in postal_routing.py
.
2. Autocorrect
Write a program in auto_correct.py
that automatically corrects words. This
program takes two arguments on the command line:
- an input file of text
- an output file
An example of the input file is in "sloppy.txt
:
i saw yuo adn the dog at teh park
did you see what thye were doing there
You are given a dictionary that maps incorrect words to correct words:
corrections = {
'teh': 'the',
'adn': 'and',
'thye': 'they',
'yuo': 'you',
'i': 'I'
}
Your program should go through each word in every line of the input file, find
any words in the corrections
dictionary, and then map those words to their
correct word. The program should write the corrected lines to the output file.
Given the above dictionary, if you run the program:
python auto_correct.py sloppy.txt neat.txt
then the file neat.txt
should contains:
I saw you and the dog at the park
did you see what they were doing there
We have given you starter code in auto_correct.py
.
Tests
Be sure you can pass the tests before you turn in the assignment. Review the guide on using pytest if you need to review using pytest and what to do if a test fails.
Grading
Activity | Points |
---|---|
Postal routing | 10 |
Autocorrect | 10 |