Please write program P5.25 in Python postal bar codes. For faster sorting of let
ID: 3918785 • Letter: P
Question
Please write program P5.25 in Python
postal bar codes. For faster sorting of letters, the United States Postal Service encourages companies that send large volumes of mail to use a bar code denoting the zip code (see Figure 10). The encoding scheme for a five-digit zip code is shown in Figure 11. There are full-height frame bars on each side. The five encoded digits are followed by a check digit, which is computed as follows: Add up all digits, and choose the check digit to
make the sum a multiple of 10. For example, the zip code 95014 has a sum of 19, so the check digit is 1 to make the sum equal to 20. Each digit of the zip code, and the check digit, is encoded according to the table below, where 0 denotes a half bar and 1 a full bar: Digit Bar 1 (weight 7) Bar 2 (weight 4) Bar 3 (weight 2) Bar 4 (weight 1) Bar 5 (weight 0).
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 1
7 1 0 0 1 0
8 1 0 1 0 0
9 1 1 0 0 0
The digit can be computed easily from the bar code using the column weights 7, 4, 2, 1, 0. For example, 01100 is 0 × 7 + 1 × 4 + 1 × 2 + 0 × 1 + 0 × 0 = 6. The only exception is 0, which would yield 11 according to the weight formula. Write a program that asks the user for a zip code and prints the bar code. Use: for half bars, | for full bars. For example, 95014 becomes ||:|:::|:|:||::::::||:|::|:::|||
Provide these functions: def printDigit(d) def printBarCode(zipCode)
Explanation / Answer
def printDigit(d):
s = ""
res = ""
i = 1
while i < len(d):
s = d[i:i+5]
if s == "11000":
res = res+"0"
elif s == "10100":
res = res+"9"
elif s == "10010":
res = res+"8"
elif s == "10001":
res = res+"7"
elif s == "01100":
res = res+"9"
elif s == "01010":
res = res+"5"
elif s == "01001":
res = res+"4"
elif s == "00110":
res = res+"3"
elif s == "00101":
res = res+"2"
elif s == "00011":
res = res+"1"
i = i+5
print("ZipCode : ",res)
def printBarCode(zipCode):
z = str(zipCode)
n = 0
for i in range(len(z)):
n = n + int(z[i])
if n%10 == 0:
z = z+"0"
else:
n = n%10
n = 10 - n
z = z + str(n)
res = "|"
for i in range(len(z)):
if z[i] == "9":
res = res + "|:|:::"
elif z[i] == "8":
res = res + "|::|:"
elif z[i] == "7":
res = res + "|:::|"
elif z[i] == "6":
res = res + ":||::"
elif z[i] == "5":
res = res + ":|:|:"
elif z[i] == "4":
res = res + ":|::|"
elif z[i] == "3":
res = res + "::||:"
elif z[i] == "2":
res = res + "::|:|"
elif z[i] == "1":
res = res + ":::||"
elif z[i] == "0":
res = res + "||:::"
res = res + "|"
print("BarCode : ",res)
def main():
z = input("Enter a zip code (5 digit): ")
printBarCode(z)
main()