UPC

From Rosetta Code
Revision as of 18:00, 25 November 2019 by Rdm (talk | contribs) (Created page with "{{draft task}} Task: Convert upc barcodes to decimal. Specifically: The UPC standard is actually a collection of standards -- physical standard...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
UPC is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Task: Convert upc barcodes to decimal.

Specifically:

The 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 barcodes to ascii, with spaces and # characters representing the presence or absence of ink.

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

         # #   # ##  #  ## #   ## ### ## ### ## #### # # # ## ##  #   #  ##  ## ###  # ##  ## ### #  # #       
        # # #   ##   ## # #### #   # ## #   ## #   ## # # # ###  # ###  ##  ## ###  # #  ### ###  # # #         
         # #    # # #  ###  #   #    # #  #   #    # # # # ## #   ## #   ## #   ##   # # #### ### ## # #         
       # # ##  ## ##  ##   #  #   #  # ###  # ##  ## # # #   ## ##  #  ### ## ## #   # #### ## #   # #        
         # # ### ## #   ## ## ###  ##  # ##   #   # ## # # ### #  ## ##  #    # ### #  ## ##  #      # #          
          # #  #   # ##  ##  #   #   #  # ##  ##  #   # # # # #### #  ##  # #### #### # #  ##  # #### # #         
         # #  #  ##  ##  # #   ## ##   # ### ## ##   # # # #  #   #   #  #  ### # #    ###  # #  #   # #        
        # # #    # ##  ##   #  # ##  ##  ### #   #  # # # ### ## ## ### ## ### ### ## #  ##  ### ## # #         
         # # ### ##   ## # # #### #   ## # #### # #### # # #   #  # ###  #    # ###  # #    # ###  # # #       
        # # # #### ##   # #### # #   ## ## ### #### # # # #  ### # ###  ###  # # ###  #    # #  ### # #         

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

The task is to 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).

Notes:

Each digit is represented by 7 bits:

  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

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 (alternatively: spaces always represent zeros and # characters always represent ones, but the representation is logically negated -- 1s and 0s are flipped -- on the right hand side of the bar code).

The UPC-A barcode structure begins with at least 9 spaces (which our imaginary bar code reader unfortunately doesn't always reproduce properly), then has a '# #' sequence marking the start of the sequence, then has the six "left hand" digits, then has a ' # # ' sequence in the middle, then has the six "right hand digits" and finally ends with another '# #' end sequence and nine trailing spaces (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. 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 (must have a zero as its last digit) if the number has been read correctly.