Task: UPC


C++: #include <iostream>
#include <locale>
#include <map>
#include <vector>

std::string trim(const std::string &str) {
    auto s = str;

    
    auto it1 = std::find_if(s.rbegin(), s.rend(), [](char ch) { return !std::isspace<char>(ch, std::locale::classic()); });
    s.erase(it1.base(), s.end());

    
    auto it2 = std::find_if(s.begin(), s.end(), [](char ch) { return !std::isspace<char>(ch, std::locale::classic()); });
    s.erase(s.begin(), it2);

    return s;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    auto it = v.cbegin();
    auto end = v.cend();

    os << '[';
    if (it != end) {
        os << *it;
        it = std::next(it);
    }
    while (it != end) {
        os << ", " << *it;
        it = std::next(it);
    }
    return os << ']';
}

const std::map<std::string, int> LEFT_DIGITS = {
    {"   ## #", 0},
    {"  ##  #", 1},
    {"  #  ##", 2},
    {" #### #", 3},
    {" #   ##", 4},
    {" ##   #", 5},
    {" # ####", 6},
    {" ### ##", 7},
    {" ## ###", 8},
    {"   # ##", 9}
};

const std::map<std::string, int> RIGHT_DIGITS = {
    {"###  # ", 0},
    {"##  ## ", 1},
    {"## ##  ", 2},
    {"#    # ", 3},
    {"# ###  ", 4},
    {"#  ### ", 5},
    {"# #    ", 6},
    {"#   #  ", 7},
    {"#  #   ", 8},
    {"### #  ", 9}
};

const std::string END_SENTINEL = "# #";
const std::string MID_SENTINEL = " # # ";

void decodeUPC(const std::string &input) {
    auto decode = [](const std::string &candidate) {
        using OT = std::vector<int>;
        OT output;
        size_t pos = 0;

        auto part = candidate.substr(pos, END_SENTINEL.length());
        if (part == END_SENTINEL) {
            pos += END_SENTINEL.length();
        } else {
            return std::make_pair(false, OT{});
        }

        for (size_t i = 0; i < 6; i++) {
            part = candidate.substr(pos, 7);
            pos += 7;

            auto e = LEFT_DIGITS.find(part);
            if (e != LEFT_DIGITS.end()) {
                output.push_back(e->second);
            } else {
                return std::make_pair(false, output);
            }
        }

        part = candidate.substr(pos, MID_SENTINEL.length());
        if (part == MID_SENTINEL) {
            pos += MID_SENTINEL.length();
        } else {
            return std::make_pair(false, OT{});
        }

        for (size_t i = 0; i < 6; i++) {
            part = candidate.substr(pos, 7);
            pos += 7;

            auto e = RIGHT_DIGITS.find(part);
            if (e != RIGHT_DIGITS.end()) {
                output.push_back(e->second);
            } else {
                return std::make_pair(false, output);
            }
        }

        part = candidate.substr(pos, END_SENTINEL.length());
        if (part == END_SENTINEL) {
            pos += END_SENTINEL.length();
        } else {
            return std::make_pair(false, OT{});
        }

        int sum = 0;
        for (size_t i = 0; i < output.size(); i++) {
            if (i % 2 == 0) {
                sum += 3 * output[i];
            } else {
                sum += output[i];
            }
        }
        return std::make_pair(sum % 10 == 0, output);
    };

    auto candidate = trim(input);

    auto out = decode(candidate);
    if (out.first) {
        std::cout << out.second << '\n';
    } else {
        std::reverse(candidate.begin(), candidate.end());
        out = decode(candidate);
        if (out.first) {
            std::cout << out.second << " Upside down\n";
        } else if (out.second.size()) {
            std::cout << "Invalid checksum\n";
        } else {
            std::cout << "Invalid digit(s)\n";
        }
    }
}

int main() {
    std::vector<std::string> barcodes = {
        "         # #   # ##  #  ## #   ## ### ## ### ## #### # # # ## ##  #   #  ##  ## ###  # ##  ## ### #  # #       ",
        "        # # #   ##   ## # #### #   # ## #   ## #   ## # # # ###  # ###  ##  ## ###  # #  ### ###  # # #         ",
        "         # #    # # #  ###  #   #    # #  #   #    # # # # ## #   ## #   ## #   ##   # # #### ### ## # #         ",
        "       # # ##  ## ##  ##   #  #   #  # ###  # ##  ## # # #   ## ##  #  ### ## ## #   # #### ## #   # #        ",
        "         # # ### ## #   ## ## ###  ##  # ##   #   # ## # # ### #  ## ##  #    # ### #  ## ##  #      # #          ",
        "          # #  #   # ##  ##  #   #   #  # ##  ##  #   # # # # #### #  ##  # #### #### # #  ##  # #### # #         ",
        "         # #  #  ##  ##  # #   ## ##   # ### ## ##   # # # #  #   #   #  #  ### # #    ###  # #  #   # #        ",
        "        # # #    # ##  ##   #  # ##  ##  ### #   #  # # # ### ## ## ### ## ### ### ## #  ##  ### ## # #         ",
        "         # # ### ##   ## # # #### #   ## # #### # #### # # #   #  # ###  #    # ###  # #    # ###  # # #       ",
        "        # # # #### ##   # #### # #   ## ## ### #### # # # #  ### # ###  ###  # # ###  #    # #  ### # #         ",
    };
    for (auto &barcode : barcodes) {
        decodeUPC(barcode);
    }
    return 0;
}







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