At the beginning of the current century the post oce was usi

At the beginning of the current century the post oce was using a bar code system to help machines sort letters. A bar code was created using the digits of a zip code. Each digit of a zip code had a bar code representation consisting of a sequence of tall and short vertical bars. The bar code was constructed by essentially substituting each digit with its bar code representation.
There were three dierent types of zip codes which were used and each could be converted into a bar code. The rst was the early short 5 digit zip code, such as 67218. The second was the extended zip code most commonly used at the time, such as 67218-1234. Think of the 67218 as the zone or area code and 1234 as a location within the zone. The third was the advanced bar code which adds the last two digits of your street address or post oce box number to the popular standard, such as 67218-1234+12. These last two digits were referred to as the “delivery point”. We can consider the last two digits to be kind of a check for human interpretation.
The format for a bar code was: |Z[P[D]]C| where the outer ”|” s are vertical bar characters, Z is the 5 digit encoded bar code, P is the optional 4 digit extended encoded bar code, and D is the optional 2 digit delivery point encoded. C is a check sum digit which is bar encoded. It was required for all bar codes. The check sum was the unique number, which when added to the sum of all the other digits, will make the sum a multiple of 10. For example, if the zip code is 67218, then 6 + 7 + 2 + 1 + 8 = 24, so the check sum digit must be 6 to make 30.
We will represent short vertical bars by a ‘.’ and the tall vertical bars by a ‘!’. Therefore the bar code representations for the digits would be as follows:
1 = ...!!

2 = ..!.!

3 = ..!!.

4 = .!..!

5 = .!.!.

6 = .!!..

7 = !...!

8 = !..!.

9 = !.!..

0 = !!...
The bar codes for the three examples we used above would be:
67218 = !.!!..!...!..!.!...!!!..!..!!..!

67218-1234 = !.!!..!...!..!.!...!!!..!....!!..!.!..!!..!..!.!!..!

67218-1234+12 = !.!!..!...!..!.!...!!!..!....!!..!.!..!!..!..!...!!..!.!..!!.!
Notice that a short bar code required 32 bars, a current standard bar code required 52 bars, and an advanced bar code required 62 bars.
Program Objective One:
Write a Python program which will read in zip codes from a le, one at a time, and produce a bar code for each zip code, such as done in the examples above. The zip codes will be stored in a le called zip_codes. Write your program output to a le called bar_codes. Write a function to perform this encoding task.
Program Objective Two:
Extend your program to add the capability to read bar codes and convert them back into readable zip codes. Use your program output from above to check your work. When decoding bar codes, your program should use an input le named bar_codes and write to an output le called zip_codes. Your program should prompt the user as to whether he or she wishes to encode zip codes or decode bar codes. Write a function to perform this decoding task.

Solution

#helper function to encode zipcode
def zipToBar(zipCode):
currentBarCode = \'!\'
#remove the - and + symbols, and then remove a newline \'\ \' from the end of the string
zipCode = zipCode.replace(\'-\',\'\').replace(\'+\',\'\').rstrip()
digitSum = 0
#iterate over all the characters of the zipcode
for digitChar in zipCode:
digit = int(digitChar)
#append the corresponding code of the digit
currentBarCode += digitToCode[digit]
#add digit to get digit sum of zipcode
digitSum += digit
#append the code of the checksum, digitSum%10 gives the last digit
#difference of the last digit with 10 is the checksum
currentBarCode += digitToCode[10 - digitSum%10] + \'!\ \'
return currentBarCode

def barToZip(barCode):
#remove the newline character from the right end
barCode = barCode.rstrip()
#remove the starting and ending !
barCode = barCode[1:-1]
#numDigits in decoded zipcode is length of encoded barcode divided by 5
numDigits = len(barCode)/5
#idx1 and idx2 are indices to get substrings of length 5 from the barcode
idx1,idx2 = 0,5
zipCode = \'\'
#iterated numDIgits -1 times since the last digit (checksum) not to be included in zipcode
for _ in range(numDigits-1):
#getting the current 5 length code
zipCode += codeToDigit[barCode[idx1:idx2]]
#updating idx1 and idx2 to get next 5 length code
idx1 = idx2
idx2 += 5
l = len(zipCode)
#adding the + and - symbols at appropriate places
#if zipCode of third type
if(l>9):
zipCode = zipCode[0:5] + \"-\" + zipCode[5:9] + \"+\" + zipCode[9:]
#zipcode of second type
elif(l>5):
zipCode = zipCode[0:5] + \"-\" + zipCode[5:]
return zipCode + \"\ \"


digitToCode = {}
digitToCode[1] = \'...!!\'
digitToCode[2] = \'..!.!\'
digitToCode[3] = \'..!!.\'
digitToCode[4] = \'.!..!\'
digitToCode[5] = \'.!.!.\'
digitToCode[6] = \'.!!..\'
digitToCode[7] = \'!...!\'
digitToCode[8] = \'!..!.\'
digitToCode[9] = \'!.!..\'
digitToCode[0] = \'!!...\'

codeToDigit = {}
codeToDigit[\'...!!\'] = \'1\'
codeToDigit[\'..!.!\'] = \'2\'
codeToDigit[\'..!!.\'] = \'3\'
codeToDigit[\'.!..!\'] = \'4\'
codeToDigit[\'.!.!.\'] = \'5\'
codeToDigit[\'.!!..\'] = \'6\'
codeToDigit[\'!...!\'] = \'7\'
codeToDigit[\'!..!.\'] = \'8\'
codeToDigit[\'!.!..\'] = \'9\'
codeToDigit[\'!!...\'] = \'0\'

# The main code starts here
#Files zip_codes and bar_codes are assumed to be
#in the same directory as the module
#they can be shifted everywhere
#provided the path is also changed

choice = raw_input(\"Input 1 for encode and 2 for decode\ \")
if(choice == \'1\'):
inputFile = open(\'zip_codes\',\'r\')
outputFile = open(\'bar_codes\',\'w\')

for zipCode in inputFile:
outputFile.write(zipToBar(zipCode))

inputFile.close()
outputFile.close()
elif(choice == \'2\'):
inputFile = open(\'bar_codes\',\'r\')
outputFile = open(\'zip_codes\',\'w\')
  
for barCode in inputFile:
outputFile.write(barToZip(barCode))
inputFile.close()
outputFile.close()
else:
print \"Please enter either 1 or 2 as input\"

  

At the beginning of the current century the post oce was using a bar code system to help machines sort letters. A bar code was created using the digits of a zip
At the beginning of the current century the post oce was using a bar code system to help machines sort letters. A bar code was created using the digits of a zip
At the beginning of the current century the post oce was using a bar code system to help machines sort letters. A bar code was created using the digits of a zip

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site