RosettaCodeData/Task/UPC/00-TASK.txt

74 lines
3.8 KiB
Plaintext

;Goal:
Convert UPC bar codes to decimal.
Specifically:
The [[wp:Universal_Product_Code|UPC]] standard is actually a collection of standards -- physical standards, data format standards, product reference standards...
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).
;Sample input:
Below, we have a representation of ten different UPC-A bar codes read by our imaginary bar code reader:
<pre>
# # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # #
# # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # #
# # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # #
# # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # #
# # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # #
# # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # #
# # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # #
# # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # #
# # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # #
# # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # #
</pre>
Some of these were entered upside down, &nbsp; and one entry has a timing error.
;Task:
Implement code to find the corresponding decimal representation of each, rejecting the error.
Extra credit for handling the rows entered upside down &nbsp; (the other option is to reject them).
;Notes:
Each digit is represented by 7 bits:
<pre>
0: 0 0 0 1 1 0 1
1: 0 0 1 1 0 0 1
2: 0 0 1 0 0 1 1
3: 0 1 1 1 1 0 1
4: 0 1 0 0 0 1 1
5: 0 1 1 0 0 0 1
6: 0 1 0 1 1 1 1
7: 0 1 1 1 0 1 1
8: 0 1 1 0 1 1 1
9: 0 0 0 1 0 1 1
</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.
;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.
;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>