Question: United States Postal Service uses a bar code encoding system calledPOSTNETto represent 5-digit ZIP codes. In this case, each digit of the ZIP code plus
United States Postal Service uses a bar code encoding system calledPOSTNETto represent 5-digit ZIP codes. In this case, each digit of the ZIP code plus thecheck digitare represented with a series of five full or half vertical bars. Each decimal digit is represented according to the values in the data file named postnet-encoding.dat data file, which contains six columns of data on each row.
The first column specifies the decimal digit to be encoded, and the remaining five columns specify the height of the bars that will represent that digit. For example, if you look at the row for decimal digit 8, we have 8 1 0 0 1 0, which means 8 is to be encoded as 1 0 0 1 0, where a 1 indicates a full bar and a 0 indicates half a bar. So, for example, ZIP code digit 8 would be represented as
To represent 5-digit ZIP codes, one must do the following:
- Add the individual digits of the provided ZIP code. For example, the sum of the digits of the ZIP code 97341 is 24
- Compute the remainder of this total when integer divided by 10. For the total 24, the remainder from the division by 10 would be 4.
- Subtract the value from STEP 2 from 10. The result will be yourcheck digit. For our example, our check digit would be 10 4, which is 6.
Finally, to generate the bar code, you will do the following:
- Generate a full bar to indicate the start of the bar code
- Add the 5-bar representation of each of the five digits of the ZIP code
- Add the 5-bar representation of thecheck digit
- Generate a full bar to indicate the end of the bar code
For example, the ZIP code 97341 will be represented by the 5-bar representation of digits 9, 7, 3, 4, 1, 6. Therefore, with the addition of one full bar at the beginning and end, a POSTNET barcode will have 32 bars.
Now write a Python function called postnet_barcode( zipcode, encoding, fbc='', hbc='' ), which takes the ZIP code as an integer, zipcode. The encoding parameter will be adictionary, and its contents will be returned by a second function you need to write named read_postnet_encoding (See below). The full bar character defaults to the provided value of the fbc parameter, and the half bar character defaults to the provided value of the hbc parameter. Your function must return astringwith the POSTNET barcode of the given ZIP code. Finally, postnet_barcode() mustraisean AssertionError when the ZIP code does not have five digits exactly.
You will write a Python function called read_postnet_encoding( data_filepath ) that will read the POSTNET encoding table from postnet-encoding.dat file and return adictionarythat will map each of the ten ZIP code digits to alistof numeric 1's and 0's, as specified in the encoding data file, in the same left-to-right order.
For example, the value for ZIP digit 8 should be represented as
{
.
.
.
8: [1, 0, 0, 1, 0],
.
.
.
}
in the dictionary that read_postnet_encoding function will return.
See the test cases for more information.
%%file postnet-encoding.dat 1 0 0 0 1 1 2 0 0 1 0 1 3 0 0 1 1 0 4 0 1 0 0 1 5 0 1 0 1 0 6 0 1 1 0 0 7 1 0 0 0 1 8 1 0 0 1 0 9 1 0 1 0 0 0 1 1 0 0 0
def read_postnet_encoding( data_filepath ): """ See specifications above """ # YOUR CODE HERE pass
def postnet_barcode( zipcode, encoding, fbc='', hbc='' ): """ See specifications above """ # YOUR CODE HERE pass
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
