BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

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.

Hint: You can grab the last element of list with name_of_list[-1]. For example:

address = ['9927', 'Giffin', 'Ct,', 'Windermere,', 'FL', '34786']
address[-1] = '34786'

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.

It is recommended that you decompose this problem in similar way to the “Doubling a recipe” activity from lab 4e.

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

ActivityPoints
Postal routing10
Autocorrect10

Manual Grading

Refer to the Quality Code guide page for detailed explanations and examples on each of these rubric criteria.

Intent

The intent of this assignment is that you know how to access the value for a given key in a dictionary. Make sure you remember this skill for future assignments.

Using a global variable is generally bad practice if the variable could easily be passed as an argument instead. However, in postal routing, the dictionary we provide never changes, and it’s awkward to put it inside a function. On this assignment, we won’t dock points for where you put the dictionary, meaning you can use it as a global variable or include it inside a function.

RubricPoints
Whitespace1
Naming1
Decomposition4
Intent4
Total10