UPC: Difference between revisions

Content added Content deleted
m (added highlighting and whitespace, corrected some misspellings.)
m (added ;Goal:, ;Task;, ;Sample input:, ;Verification; section headers, split the UPC-A bar code structure into separate bullet points, added whitespace.)
Line 1: Line 1:
{{draft task}}
{{draft task}}

;Task:
;Goal:
Convert UPC bar codes to decimal.
Convert UPC bar codes to decimal.


Line 8: Line 9:
The [[wp:Universal_Product_Code|UPC]] standard is actually a collection of standards -- physical standards, data format standards, product reference standards...
The [[wp:Universal_Product_Code|UPC]] standard is actually a collection of standards -- physical standards, data format standards, product reference standards...


Here, we focus on some of the data format standards, with an imaginary physical+electrical implementation which converts physical UPC bar codes to ASCII with spaces and '''#''' characters representing the presence or absence of ink.
Here,   in this task,   we will focus on some of the data format standards,   with an imaginary physical+electrical implementation which converts physical UPC bar codes to ASCII   (with spaces and   '''#'''   characters representing the presence or absence of ink).


Below, we have a representation of 10 different UPC-A bar codes read by our imaginary bar code reader:


;Sample input:
Below, we have a representation of ten different UPC-A bar codes read by our imaginary bar code reader:
<pre>
<pre>
# # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # #
# # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # #
Line 24: Line 26:
# # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # #
# # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # #
</pre>
</pre>
Some of these were entered upside down, &nbsp; and one entry has a timing error.


Some of these were input upside down, and one has a timing error.


;Task:
The task is to implement code to find the corresponding decimal representation of each, rejecting the error.
Implement code to find the corresponding decimal representation of each, rejecting the error.


Extra credit for handling the rows entered upside down (the other option is to reject them).
Extra credit for handling the rows entered upside down &nbsp; (the other option is to reject them).




Notes:
;Notes:

Each digit is represented by 7 bits:
Each digit is represented by 7 bits:

<pre>
<pre>
0: 0 0 0 1 1 0 1
0: 0 0 0 1 1 0 1
1: 0 0 1 1 0 0 1
1: 0 0 1 1 0 0 1
2: 0 0 1 0 0 1 1
2: 0 0 1 0 0 1 1
3: 0 1 1 1 1 0 1
3: 0 1 1 1 1 0 1
4: 0 1 0 0 0 1 1
4: 0 1 0 0 0 1 1
5: 0 1 1 0 0 0 1
5: 0 1 1 0 0 0 1
6: 0 1 0 1 1 1 1
6: 0 1 0 1 1 1 1
7: 0 1 1 1 0 1 1
7: 0 1 1 1 0 1 1
8: 0 1 1 0 1 1 1
8: 0 1 1 0 1 1 1
9: 0 0 0 1 0 1 1
9: 0 0 0 1 0 1 1
</pre>
</pre>
On the left hand side of the bar code a space represents a '''0''' and a '''#''' represents a '''1'''.
<br>On the right hand side of the bar code, a '''#''' represents a '''0''' and a space represents a '''1'''
<br>Alternatively (for the above): &nbsp; spaces always represent zeros and '''#''' characters always represent ones, but the representation is logically negated -- '''1'''s and '''0'''s are flipped -- on the right hand side of the bar code.


On the left hand side of the bar code a space represents a 0 and a # represents a 1. On the right hand side of the bar code, a '''#''' represents a '''0''' and a space represents a '''1''' &nbsp; (alternatively: spaces always represent zeros and '''#''' characters always represent ones, but the representation is logically negated -- '''1'''s and '''0'''s are flipped -- on the right hand side of the bar code).



The UPC-A bar code structure begins with at least 9 spaces &nbsp; (which our imaginary bar code reader unfortunately doesn't always reproduce properly), &nbsp; then has a &nbsp; &nbsp; '''# #''' &nbsp; &nbsp; sequence marking the start of the sequence, then has the six "left hand" digits, then has a &nbsp; ''' # # ''' &nbsp; sequence in the middle, then has the six "right hand digits" and finally ends with another &nbsp; '''# #''' &nbsp; end sequence and nine trailing spaces &nbsp; (which might be eaten by wiki edits and in any event were not quite captured correctly by our imaginary bar code reader).
;The UPC-A bar code structure:
::* &nbsp; It begins with at least 9 spaces &nbsp; (which our imaginary bar code reader unfortunately doesn't always reproduce properly),
::* &nbsp; then has a &nbsp; &nbsp; ''' # # ''' &nbsp; &nbsp; sequence marking the start of the sequence,
::* &nbsp; then has the six "left hand" digits,
::* &nbsp; then has a &nbsp; ''' # # ''' &nbsp; sequence in the middle,
::* &nbsp; then has the six "right hand digits",
::* &nbsp; then has another &nbsp; ''' # # ''' &nbsp; (end sequence), &nbsp; and finally,
::* &nbsp; then ends with nine trailing spaces &nbsp; (which might be eaten by wiki edits and in any event were not quite captured correctly by our imaginary bar code reader).



Finally, the last digit is a checksum digit which may be used to help detect errors.
Finally, the last digit is a checksum digit which may be used to help detect errors.



Multiply each digit in the represented 12 digit sequence by the corresponding number in (3,1,3,1,3,1,3,1,3,1,3,1) and add products, and the sum (mod 10) must be '''0''' &nbsp; (must have a zero as its last digit) &nbsp; if the number has been read correctly.
;Verification:
Multiply each digit in the represented 12 digit sequence by the corresponding number in &nbsp; (3,1,3,1,3,1,3,1,3,1,3,1) &nbsp; and add the products.

The sum (mod 10) must be '''0''' &nbsp; (must have a zero as its last digit) &nbsp; if the UPC number has been read correctly.
<br><br>
<br><br>