How to resolve the algorithm UPC step by step in the AutoHotkey programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm UPC step by step in the AutoHotkey programming language

Table of Contents

Problem Statement

Convert UPC bar codes to decimal.

Specifically: The 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).

Below, we have a representation of ten different UPC-A bar codes read by our imaginary bar code reader: Some of these were entered upside down,   and one entry has a timing 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).

Each digit is represented by 7 bits: 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 (for the above):   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.

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 the products. The sum (mod 10) must be 0   (must have a zero as its last digit)   if the UPC number has been read correctly.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm UPC step by step in the AutoHotkey programming language

Source code in the autohotkey programming language

UPC2Dec(code){
    lBits :={"   ## #":0,"  ##  #":1,"  #  ##":2," #### #":3," #   ##":4," ##   #":5," # ####":6," ### ##":7," ## ###":8,"   # ##":9}
    xlBits:={"# ##   ":0,"#  ##  ":1,"##  #  ":2,"# #### ":3,"##   # ":4,"#   ## ":5,"#### # ":6,"## ### ":7,"### ## ":8,"## #   ":9}
    rBits :={"###  # ":0,"##  ## ":1,"## ##  ":2,"#    # ":3,"# ###  ":4,"#  ### ":5,"# #    ":6,"#   #  ":7,"#  #   ":8,"### #  ":9}
    xrBits:={" #  ###":0," ##  ##":1,"  ## ##":2," #    #":3,"  ### #":4," ###  #":5,"    # #":6,"  #   #":7,"   #  #":8,"  # ###":9}
    UPC := "", CD := 0,	code := Trim(code, " ")
    S := SubStr(code, 1, 3), code := SubStr(code, 4)			; start or "upside down" end sequence
    loop 6
        C := SubStr(code, 1, 7), code := SubStr(code, 8)		; six left hand or "upside down" right hand digits
        , UPC := lBits[C] <> "" ? UPC . lBits[C] : xrBits[C] . UPC
    M := SubStr(code, 1, 5), code := SubStr(code, 6)			; middle sequence
    loop 6
        C := SubStr(code, 1, 7), code := SubStr(code, 8)		; six right hand or "upside down" left hand digits
        , UPC := rBits[C] <> "" ? UPC . rBits[C] : xlBits[C] . UPC
    E := SubStr(code, 1, 3), code := SubStr(code, 4)			; end or "upside down" start sequence
    for k, v in StrSplit(UPC)
        CD += Mod(A_Index, 2) ? v*3 : v					; Check Digit
    if (S <> "# #") || (M <> " # # ") || (E <> "# #") || Mod(CD, 10)
        return "Invalid!"
    return UPC
}


data =
(
         # #   # ##  #  ## #   ## ### ## ### ## #### # # # ## ##  #   #  ##  ## ###  # ##  ## ### #  # #         
         # # #   ##   ## # #### #   # ## #   ## #   ## # # # ###  # ###  ##  ## ###  # #  ### ###  # # #         
         # #    # # #  ###  #   #    # #  #   #    # # # # ## #   ## #   ## #   ##   # # #### ### ## # #         
         # # ##  ## ##  ##   #  #   #  # ###  # ##  ## # # #   ## ##  #  ### ## ## #   # #### ## #   # #         
         # # ### ## #   ## ## ###  ##  # ##   #   # ## # # ### #  ## ##  #    # ### #  ## ##  #      # #         
         # #  #   # ##  ##  #   #   #  # ##  ##  #   # # # # #### #  ##  # #### #### # #  ##  # #### # #         
         # #  #  ##  ##  # #   ## ##   # ### ## ##   # # # #  #   #   #  #  ### # #    ###  # #  #   # #         
         # # #    # ##  ##   #  # ##  ##  ### #   #  # # # ### ## ## ### ## ### ### ## #  ##  ### ## # #         
         # # ### ##   ## # # #### #   ## # #### # #### # # #   #  # ###  #    # ###  # #    # ###  # # #         
         # # # #### ##   # #### # #   ## ## ### #### # # # #  ### # ###  ###  # # ###  #    # #  ### # #         
)
for i, code in StrSplit(data, "`n", "`r")
    output .= i ":`t" UPC2Dec(code) "`n"
MsgBox, 262144, ,% output
return


  

You may also check:How to resolve the algorithm Averages/Mean time of day step by step in the Scala programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the VBA programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Arturo programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Aime programming language
You may also check:How to resolve the algorithm Gray code step by step in the ALGOL 68 programming language