Task: UPC







Python: 
import itertools
import re

RE_BARCODE = re.compile(
    r"^(?P<s_quiet> +)"  
    r"(?P<s_guard>
    r"(?P<left>[ 
    r"(?P<m_guard> 
    r"(?P<right>[ 
    r"(?P<e_guard>
    r"(?P<e_quiet> +)$"  
)

LEFT_DIGITS = {
    (0, 0, 0, 1, 1, 0, 1): 0,
    (0, 0, 1, 1, 0, 0, 1): 1,
    (0, 0, 1, 0, 0, 1, 1): 2,
    (0, 1, 1, 1, 1, 0, 1): 3,
    (0, 1, 0, 0, 0, 1, 1): 4,
    (0, 1, 1, 0, 0, 0, 1): 5,
    (0, 1, 0, 1, 1, 1, 1): 6,
    (0, 1, 1, 1, 0, 1, 1): 7,
    (0, 1, 1, 0, 1, 1, 1): 8,
    (0, 0, 0, 1, 0, 1, 1): 9,
}

RIGHT_DIGITS = {
    (1, 1, 1, 0, 0, 1, 0): 0,
    (1, 1, 0, 0, 1, 1, 0): 1,
    (1, 1, 0, 1, 1, 0, 0): 2,
    (1, 0, 0, 0, 0, 1, 0): 3,
    (1, 0, 1, 1, 1, 0, 0): 4,
    (1, 0, 0, 1, 1, 1, 0): 5,
    (1, 0, 1, 0, 0, 0, 0): 6,
    (1, 0, 0, 0, 1, 0, 0): 7,
    (1, 0, 0, 1, 0, 0, 0): 8,
    (1, 1, 1, 0, 1, 0, 0): 9,
}


MODULES = {
    " ": 0,
    "
}

DIGITS_PER_SIDE = 6
MODULES_PER_DIGIT = 7


class ParityError(Exception):
    


class ChecksumError(Exception):
    


def group(iterable, n):
    
    args = [iter(iterable)] * n
    return tuple(itertools.zip_longest(*args))


def parse(barcode):
    
    match = RE_BARCODE.match(barcode)

    
    
    left = group((MODULES[c] for c in match.group("left")), MODULES_PER_DIGIT)
    right = group((MODULES[c] for c in match.group("right")), MODULES_PER_DIGIT)

    
    left, right = check_parity(left, right)

    
    return tuple(
        itertools.chain(
            (LEFT_DIGITS[d] for d in left),
            (RIGHT_DIGITS[d] for d in right),
        )
    )


def check_parity(left, right):
    
    
    
    
    left_parity = sum(sum(d) % 2 for d in left)
    right_parity = sum(sum(d) % 2 for d in right)

    
    
    if left_parity == 0 and right_parity == DIGITS_PER_SIDE:
        _left = tuple(tuple(reversed(d)) for d in reversed(right))
        right = tuple(tuple(reversed(d)) for d in reversed(left))
        left = _left
    elif left_parity != DIGITS_PER_SIDE or right_parity != 0:
        
        error = tuple(
            itertools.chain(
                (LEFT_DIGITS.get(d, "_") for d in left),
                (RIGHT_DIGITS.get(d, "_") for d in right),
            )
        )
        raise ParityError(" ".join(str(d) for d in error))

    return left, right


def checksum(digits):
    
    odds = (digits[i] for i in range(0, 11, 2))
    evens = (digits[i] for i in range(1, 10, 2))

    check_digit = (sum(odds) * 3 + sum(evens)) % 10

    if check_digit != 0:
        check_digit = 10 - check_digit

    if digits[-1] != check_digit:
        raise ChecksumError(str(check_digit))

    return check_digit


def main():
    barcodes = [
        "         
        "        
        "         
        "       
        "         
        "          
        "         
        "        
        "         
        "        
        "        
    ]

    for barcode in barcodes:
        try:
            digits = parse(barcode)
        except ParityError as err:
            print(f"{err} parity error!")
            continue

        try:
            check_digit = checksum(digits)
        except ChecksumError as err:
            print(f"{' '.join(str(d) for d in digits)} checksum error! ({err})")
            continue

        print(f"{' '.join(str(d) for d in digits)}")


if __name__ == "__main__":
    main()


VB: 

Option Explicit
Const m_limit ="# #"
Const m_middle=" # # "
Dim a,bnum,i,check,odic
a=array("         # #   # ##  #  ## #   ## ### ## ### ## #### # # # ## ##  #   #  ##  ## ###  # ##  ## ### #  # #       ",_
         "        # # #   ##   ## # #### #   # ## #   ## #   ## # # # ###  # ###  ##  ## ###  # #  ### ###  # # #         ",_
        "         # #    # # #  ###  #   #    # #  #   #    # # # # ## #   ## #   ## #   ##   # # #### ### ## # #         ",_
          "       # # ##  ## ##  ##   #  #   #  # ###  # ##  ## # # #   ## ##  #  ### ## ## #   # #### ## #   # #        ",_
        "         # # ### ## #   ## ## ###  ##  # ##   #   # ## # # ### #  ## ##  #    # ### #  ## ##  #      # #          ",_
       "          # #  #   # ##  ##  #   #   #  # ##  ##  #   # # # # #### #  ##  # #### #### # #  ##  # #### # #         ",_
        "         # #  #  ##  ##  # #   ## ##   # ### ## ##   # # # #  #   #   #  #  ### # #    ###  # #  #   # #        ",_
         "        # # #    # ##  ##   #  # ##  ##  ### #   #  # # # ### ## ## ### ## ### ### ## #  ##  ### ## # #         ",_
        "         # # ### ##   ## # # #### #   ## # #### # #### # # #   #  # ###  #    # ###  # #    # ###  # # #       ",_
         "        # # # #### ##   # #### # #   ## ## ### #### # # # #  ### # ###  ###  # # ###  #    # #  ### # #         ")


bnum=Array("0001101","0011001","0010011","0111101","0100011"," 0110001","0101111","0111011","0110111","0001011")

Set oDic = WScript.CreateObject("scripting.dictionary")
For i=0 To 9: 
  odic.Add bin2dec(bnum(i),Asc("1")),i+1 
  odic.Add bin2dec(bnum(i),Asc("0")),-i-1  
Next

For i=0 To UBound(a) : print pad(i+1,-2) & ": "& upc(a(i)) :Next
  WScript.Quit(1)
  
  Function bin2dec(ByVal B,a) 
    Dim n
    While len(b)
      n =n *2 - (asc(b)=a)  
      b=mid(b,2) 
    Wend
    bin2dec= n And 127
  End Function
  
  Sub print(s): 
    On Error Resume Next
    WScript.stdout.WriteLine (s)  
    If  err= &h80070006& Then WScript.Echo " Please run this script with CScript": WScript.quit
  End Sub
  function pad(s,n) if n<0 then pad= right(space(-n) & s ,-n) else  pad= left(s& space(n),n) end if :end function
   
  Function iif(t,a,b)  If t Then iif=a Else iif=b End If :End Function
  
  Function getnum(s,r) 
    Dim n,s1,r1 
    
    s1=Left(s,7)
    s=Mid(s,8)
    r1=r
    Do
      If r Then s1=StrReverse(s1) 
      n=bin2dec(s1,asc("#"))
      If odic.exists(n) Then
        getnum=odic(n)
        Exit Function
      Else
        If r1<>r Then getnum=0:Exit Function
        r=Not r
      End If
    Loop 
  End Function          
  
  Function getmarker(s,m) 
    getmarker= (InStr(s,m)= 1)
    s=Mid(s,Len(m)+1)
  End Function
  
  Function checksum(ByVal s)
    Dim n,i : n=0
    do
       n=n+(Asc(s)-48)*3
       s=Mid(s,2)
       n=n+(Asc(s)-48)*1
       s=Mid(s,2)
    Loop until Len(s)=0
    checksum= ((n mod 10)=0)
  End function      
      
  Function upc(ByVal s1)
    Dim i,n,s,out,rev,j 
    
    
    s=Trim(s1)
    If getmarker(s,m_limit)=False  Then upc= "bad start marker ":Exit function
    rev=False
    out=""
    For j= 0 To 1
      For i=0 To 5
        n=getnum(s,rev)
        If n=0 Then upc= pad(out,16) & pad ("bad code",-10) & pad("pos "& i+j*6+1,-11): Exit Function
        out=out & Abs(n)-1
      Next
      If j=0 Then If getmarker(s,m_middle)=False  Then upc= "bad middle marker " & out :Exit Function
    Next  
    If getmarker(s,m_limit)=False  Then upc= "bad end marker "  :Exit function
    If rev Then out=strreverse(out)
    upc= pad(out,16) &  pad(iif (checksum(out),"valid","not valid"),-10)&  pad(iif(rev,"reversed",""),-11)
  End Function