Validate a barcode

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












A barcode of EAN-13 symbology consists of 13 digits (0-9). The last digit of this barcode is its check digit. It is calculated by the following means (the barcode 8923642469559 is used as an example):




  1. Starting from the second digit, sum up all alternating digits and multiply the sum by 3:



    8 9 2 3 6 4 2 4 6 9 5 5 9
    | | | | | |
    9 + 3 + 4 + 4 + 9 + 5 = 34
    |
    34 × 3 = 102



  2. Then, sum up all of the remaining digits, but do not include the last digit:



    8 9 2 3 6 4 2 4 6 9 5 5 9
    | | | | | |
    8 + 2 + 6 + 2 + 6 + 5 = 29



  3. Add the numbers obtained in steps 1 and 2 together:



    29 + 102 = 131


  4. The number you should add to the result of step 3 to get to the next multiple of 10 (140 in this case) is the check digit.


If the check digit of the barcode matches the one calculated as explained earlier, the barcode is valid.




More examples:



6537263729385 is valid.
1902956847427 is valid.
9346735877246 is invalid. The check digit should be 3, not 6.




Your goal is to write a program that will:



  1. Receive a barcode as its input.

  2. Check whether the barcode is valid

  3. Return 1 (or equivalent) if the barcode is valid, 0 (or equivalent) otherwise.


This is code-golf, so the shortest code in terms of bytes wins.










share|improve this question























  • why did the last digit (9) become a 4
    – HyperNeutrino
    32 mins ago










  • Sorry, will fix it :)
    – Wais Kamal
    31 mins ago










  • I recommend removing the 13-char check because it's trivial but annoying in certain languages. Up to you though whether you want to do input validation (most people leave it out but you can leave it in)
    – HyperNeutrino
    31 mins ago











  • OK, I will do that.
    – Wais Kamal
    30 mins ago










  • Can we take input as a list of digits? (sorry for all the questions)
    – HyperNeutrino
    30 mins ago














up vote
2
down vote

favorite












A barcode of EAN-13 symbology consists of 13 digits (0-9). The last digit of this barcode is its check digit. It is calculated by the following means (the barcode 8923642469559 is used as an example):




  1. Starting from the second digit, sum up all alternating digits and multiply the sum by 3:



    8 9 2 3 6 4 2 4 6 9 5 5 9
    | | | | | |
    9 + 3 + 4 + 4 + 9 + 5 = 34
    |
    34 × 3 = 102



  2. Then, sum up all of the remaining digits, but do not include the last digit:



    8 9 2 3 6 4 2 4 6 9 5 5 9
    | | | | | |
    8 + 2 + 6 + 2 + 6 + 5 = 29



  3. Add the numbers obtained in steps 1 and 2 together:



    29 + 102 = 131


  4. The number you should add to the result of step 3 to get to the next multiple of 10 (140 in this case) is the check digit.


If the check digit of the barcode matches the one calculated as explained earlier, the barcode is valid.




More examples:



6537263729385 is valid.
1902956847427 is valid.
9346735877246 is invalid. The check digit should be 3, not 6.




Your goal is to write a program that will:



  1. Receive a barcode as its input.

  2. Check whether the barcode is valid

  3. Return 1 (or equivalent) if the barcode is valid, 0 (or equivalent) otherwise.


This is code-golf, so the shortest code in terms of bytes wins.










share|improve this question























  • why did the last digit (9) become a 4
    – HyperNeutrino
    32 mins ago










  • Sorry, will fix it :)
    – Wais Kamal
    31 mins ago










  • I recommend removing the 13-char check because it's trivial but annoying in certain languages. Up to you though whether you want to do input validation (most people leave it out but you can leave it in)
    – HyperNeutrino
    31 mins ago











  • OK, I will do that.
    – Wais Kamal
    30 mins ago










  • Can we take input as a list of digits? (sorry for all the questions)
    – HyperNeutrino
    30 mins ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











A barcode of EAN-13 symbology consists of 13 digits (0-9). The last digit of this barcode is its check digit. It is calculated by the following means (the barcode 8923642469559 is used as an example):




  1. Starting from the second digit, sum up all alternating digits and multiply the sum by 3:



    8 9 2 3 6 4 2 4 6 9 5 5 9
    | | | | | |
    9 + 3 + 4 + 4 + 9 + 5 = 34
    |
    34 × 3 = 102



  2. Then, sum up all of the remaining digits, but do not include the last digit:



    8 9 2 3 6 4 2 4 6 9 5 5 9
    | | | | | |
    8 + 2 + 6 + 2 + 6 + 5 = 29



  3. Add the numbers obtained in steps 1 and 2 together:



    29 + 102 = 131


  4. The number you should add to the result of step 3 to get to the next multiple of 10 (140 in this case) is the check digit.


If the check digit of the barcode matches the one calculated as explained earlier, the barcode is valid.




More examples:



6537263729385 is valid.
1902956847427 is valid.
9346735877246 is invalid. The check digit should be 3, not 6.




Your goal is to write a program that will:



  1. Receive a barcode as its input.

  2. Check whether the barcode is valid

  3. Return 1 (or equivalent) if the barcode is valid, 0 (or equivalent) otherwise.


This is code-golf, so the shortest code in terms of bytes wins.










share|improve this question















A barcode of EAN-13 symbology consists of 13 digits (0-9). The last digit of this barcode is its check digit. It is calculated by the following means (the barcode 8923642469559 is used as an example):




  1. Starting from the second digit, sum up all alternating digits and multiply the sum by 3:



    8 9 2 3 6 4 2 4 6 9 5 5 9
    | | | | | |
    9 + 3 + 4 + 4 + 9 + 5 = 34
    |
    34 × 3 = 102



  2. Then, sum up all of the remaining digits, but do not include the last digit:



    8 9 2 3 6 4 2 4 6 9 5 5 9
    | | | | | |
    8 + 2 + 6 + 2 + 6 + 5 = 29



  3. Add the numbers obtained in steps 1 and 2 together:



    29 + 102 = 131


  4. The number you should add to the result of step 3 to get to the next multiple of 10 (140 in this case) is the check digit.


If the check digit of the barcode matches the one calculated as explained earlier, the barcode is valid.




More examples:



6537263729385 is valid.
1902956847427 is valid.
9346735877246 is invalid. The check digit should be 3, not 6.




Your goal is to write a program that will:



  1. Receive a barcode as its input.

  2. Check whether the barcode is valid

  3. Return 1 (or equivalent) if the barcode is valid, 0 (or equivalent) otherwise.


This is code-golf, so the shortest code in terms of bytes wins.







code-golf arithmetic






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 21 mins ago

























asked 34 mins ago









Wais Kamal

1375




1375











  • why did the last digit (9) become a 4
    – HyperNeutrino
    32 mins ago










  • Sorry, will fix it :)
    – Wais Kamal
    31 mins ago










  • I recommend removing the 13-char check because it's trivial but annoying in certain languages. Up to you though whether you want to do input validation (most people leave it out but you can leave it in)
    – HyperNeutrino
    31 mins ago











  • OK, I will do that.
    – Wais Kamal
    30 mins ago










  • Can we take input as a list of digits? (sorry for all the questions)
    – HyperNeutrino
    30 mins ago
















  • why did the last digit (9) become a 4
    – HyperNeutrino
    32 mins ago










  • Sorry, will fix it :)
    – Wais Kamal
    31 mins ago










  • I recommend removing the 13-char check because it's trivial but annoying in certain languages. Up to you though whether you want to do input validation (most people leave it out but you can leave it in)
    – HyperNeutrino
    31 mins ago











  • OK, I will do that.
    – Wais Kamal
    30 mins ago










  • Can we take input as a list of digits? (sorry for all the questions)
    – HyperNeutrino
    30 mins ago















why did the last digit (9) become a 4
– HyperNeutrino
32 mins ago




why did the last digit (9) become a 4
– HyperNeutrino
32 mins ago












Sorry, will fix it :)
– Wais Kamal
31 mins ago




Sorry, will fix it :)
– Wais Kamal
31 mins ago












I recommend removing the 13-char check because it's trivial but annoying in certain languages. Up to you though whether you want to do input validation (most people leave it out but you can leave it in)
– HyperNeutrino
31 mins ago





I recommend removing the 13-char check because it's trivial but annoying in certain languages. Up to you though whether you want to do input validation (most people leave it out but you can leave it in)
– HyperNeutrino
31 mins ago













OK, I will do that.
– Wais Kamal
30 mins ago




OK, I will do that.
– Wais Kamal
30 mins ago












Can we take input as a list of digits? (sorry for all the questions)
– HyperNeutrino
30 mins ago




Can we take input as a list of digits? (sorry for all the questions)
– HyperNeutrino
30 mins ago










6 Answers
6






active

oldest

votes

















up vote
2
down vote














Ruby, 45 40 bytes





->nn.gsub(/(.(.?))/,'122').sum%10<1


Try it online!



How it works:



Iterate on the string, triplicating every second character. The sum of the ASCII codes of the string modulo 10 is the same as the checksum we are looking for (the difference is 48*7+48*6*3==1200 so it doesn't matter)






share|improve this answer





























    up vote
    1
    down vote














    05AB1E, 15 10 bytes



    εNÉ·>*}OTÖ


    Try it online!



    Explanation



     ε } # apply to each digit
    NÉ # is the current index odd?
    ·> # double and increment(yielding 1 or 3)
    * # multiply by the current number
    O # sum all modified digits
    TÖ # is evenly divisible by 10





    share|improve this answer






















    • Nice answer! Definitely shorter than what I was working on. I like the x you've used in combination with O to sum the entire stack in one go to combine the *3 and + of the two numbers. As well as the (T% to get the remainder.
      – Kevin Cruijssen
      16 mins ago











    • @KevinCruijssen: The comparison feels a bit surperflous. Can you think of a counterexample to εNÉ·>*}OTÖ working?
      – Emigna
      11 mins ago











    • Hmm, not really. It would be useful to have more test cases than the 4 we have now. Can't really think of anything where S13S13∍*OTÖ would fail right now (so well done golfing 4 bytes). It also seems similar as the Python answer (and Jelly as well I think; I can't really read Jelly all that well).
      – Kevin Cruijssen
      7 mins ago


















    up vote
    0
    down vote














    Python 3, 55 bytes





    lambda s:not sum(int(i)*d for i,d in zip(s,[1,3]*7))%10


    Try it online!






    share|improve this answer



























      up vote
      0
      down vote














      Jelly, 14 bytes



      D;0×1,3ẋ7¤S%⁵¬


      Try it online!






      share|improve this answer





























        up vote
        0
        down vote














        Perl 6, 29 bytes





        :1[map 3×*+*,0,


        Try it online!





        share



























          up vote
          0
          down vote














          Japt -!, 13 bytes



          Ë*(Ev ª3Ãx %A


          Try it online!





          share






















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "200"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174495%2fvalidate-a-barcode%23new-answer', 'question_page');

            );

            Post as a guest






























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote














            Ruby, 45 40 bytes





            ->nn.gsub(/(.(.?))/,'122').sum%10<1


            Try it online!



            How it works:



            Iterate on the string, triplicating every second character. The sum of the ASCII codes of the string modulo 10 is the same as the checksum we are looking for (the difference is 48*7+48*6*3==1200 so it doesn't matter)






            share|improve this answer


























              up vote
              2
              down vote














              Ruby, 45 40 bytes





              ->nn.gsub(/(.(.?))/,'122').sum%10<1


              Try it online!



              How it works:



              Iterate on the string, triplicating every second character. The sum of the ASCII codes of the string modulo 10 is the same as the checksum we are looking for (the difference is 48*7+48*6*3==1200 so it doesn't matter)






              share|improve this answer
























                up vote
                2
                down vote










                up vote
                2
                down vote










                Ruby, 45 40 bytes





                ->nn.gsub(/(.(.?))/,'122').sum%10<1


                Try it online!



                How it works:



                Iterate on the string, triplicating every second character. The sum of the ASCII codes of the string modulo 10 is the same as the checksum we are looking for (the difference is 48*7+48*6*3==1200 so it doesn't matter)






                share|improve this answer















                Ruby, 45 40 bytes





                ->nn.gsub(/(.(.?))/,'122').sum%10<1


                Try it online!



                How it works:



                Iterate on the string, triplicating every second character. The sum of the ASCII codes of the string modulo 10 is the same as the checksum we are looking for (the difference is 48*7+48*6*3==1200 so it doesn't matter)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 11 mins ago

























                answered 25 mins ago









                G B

                7,2471327




                7,2471327




















                    up vote
                    1
                    down vote














                    05AB1E, 15 10 bytes



                    εNÉ·>*}OTÖ


                    Try it online!



                    Explanation



                     ε } # apply to each digit
                    NÉ # is the current index odd?
                    ·> # double and increment(yielding 1 or 3)
                    * # multiply by the current number
                    O # sum all modified digits
                    TÖ # is evenly divisible by 10





                    share|improve this answer






















                    • Nice answer! Definitely shorter than what I was working on. I like the x you've used in combination with O to sum the entire stack in one go to combine the *3 and + of the two numbers. As well as the (T% to get the remainder.
                      – Kevin Cruijssen
                      16 mins ago











                    • @KevinCruijssen: The comparison feels a bit surperflous. Can you think of a counterexample to εNÉ·>*}OTÖ working?
                      – Emigna
                      11 mins ago











                    • Hmm, not really. It would be useful to have more test cases than the 4 we have now. Can't really think of anything where S13S13∍*OTÖ would fail right now (so well done golfing 4 bytes). It also seems similar as the Python answer (and Jelly as well I think; I can't really read Jelly all that well).
                      – Kevin Cruijssen
                      7 mins ago















                    up vote
                    1
                    down vote














                    05AB1E, 15 10 bytes



                    εNÉ·>*}OTÖ


                    Try it online!



                    Explanation



                     ε } # apply to each digit
                    NÉ # is the current index odd?
                    ·> # double and increment(yielding 1 or 3)
                    * # multiply by the current number
                    O # sum all modified digits
                    TÖ # is evenly divisible by 10





                    share|improve this answer






















                    • Nice answer! Definitely shorter than what I was working on. I like the x you've used in combination with O to sum the entire stack in one go to combine the *3 and + of the two numbers. As well as the (T% to get the remainder.
                      – Kevin Cruijssen
                      16 mins ago











                    • @KevinCruijssen: The comparison feels a bit surperflous. Can you think of a counterexample to εNÉ·>*}OTÖ working?
                      – Emigna
                      11 mins ago











                    • Hmm, not really. It would be useful to have more test cases than the 4 we have now. Can't really think of anything where S13S13∍*OTÖ would fail right now (so well done golfing 4 bytes). It also seems similar as the Python answer (and Jelly as well I think; I can't really read Jelly all that well).
                      – Kevin Cruijssen
                      7 mins ago













                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote










                    05AB1E, 15 10 bytes



                    εNÉ·>*}OTÖ


                    Try it online!



                    Explanation



                     ε } # apply to each digit
                    NÉ # is the current index odd?
                    ·> # double and increment(yielding 1 or 3)
                    * # multiply by the current number
                    O # sum all modified digits
                    TÖ # is evenly divisible by 10





                    share|improve this answer















                    05AB1E, 15 10 bytes



                    εNÉ·>*}OTÖ


                    Try it online!



                    Explanation



                     ε } # apply to each digit
                    NÉ # is the current index odd?
                    ·> # double and increment(yielding 1 or 3)
                    * # multiply by the current number
                    O # sum all modified digits
                    TÖ # is evenly divisible by 10






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 4 mins ago

























                    answered 22 mins ago









                    Emigna

                    43.9k431133




                    43.9k431133











                    • Nice answer! Definitely shorter than what I was working on. I like the x you've used in combination with O to sum the entire stack in one go to combine the *3 and + of the two numbers. As well as the (T% to get the remainder.
                      – Kevin Cruijssen
                      16 mins ago











                    • @KevinCruijssen: The comparison feels a bit surperflous. Can you think of a counterexample to εNÉ·>*}OTÖ working?
                      – Emigna
                      11 mins ago











                    • Hmm, not really. It would be useful to have more test cases than the 4 we have now. Can't really think of anything where S13S13∍*OTÖ would fail right now (so well done golfing 4 bytes). It also seems similar as the Python answer (and Jelly as well I think; I can't really read Jelly all that well).
                      – Kevin Cruijssen
                      7 mins ago

















                    • Nice answer! Definitely shorter than what I was working on. I like the x you've used in combination with O to sum the entire stack in one go to combine the *3 and + of the two numbers. As well as the (T% to get the remainder.
                      – Kevin Cruijssen
                      16 mins ago











                    • @KevinCruijssen: The comparison feels a bit surperflous. Can you think of a counterexample to εNÉ·>*}OTÖ working?
                      – Emigna
                      11 mins ago











                    • Hmm, not really. It would be useful to have more test cases than the 4 we have now. Can't really think of anything where S13S13∍*OTÖ would fail right now (so well done golfing 4 bytes). It also seems similar as the Python answer (and Jelly as well I think; I can't really read Jelly all that well).
                      – Kevin Cruijssen
                      7 mins ago
















                    Nice answer! Definitely shorter than what I was working on. I like the x you've used in combination with O to sum the entire stack in one go to combine the *3 and + of the two numbers. As well as the (T% to get the remainder.
                    – Kevin Cruijssen
                    16 mins ago





                    Nice answer! Definitely shorter than what I was working on. I like the x you've used in combination with O to sum the entire stack in one go to combine the *3 and + of the two numbers. As well as the (T% to get the remainder.
                    – Kevin Cruijssen
                    16 mins ago













                    @KevinCruijssen: The comparison feels a bit surperflous. Can you think of a counterexample to εNÉ·>*}OTÖ working?
                    – Emigna
                    11 mins ago





                    @KevinCruijssen: The comparison feels a bit surperflous. Can you think of a counterexample to εNÉ·>*}OTÖ working?
                    – Emigna
                    11 mins ago













                    Hmm, not really. It would be useful to have more test cases than the 4 we have now. Can't really think of anything where S13S13∍*OTÖ would fail right now (so well done golfing 4 bytes). It also seems similar as the Python answer (and Jelly as well I think; I can't really read Jelly all that well).
                    – Kevin Cruijssen
                    7 mins ago





                    Hmm, not really. It would be useful to have more test cases than the 4 we have now. Can't really think of anything where S13S13∍*OTÖ would fail right now (so well done golfing 4 bytes). It also seems similar as the Python answer (and Jelly as well I think; I can't really read Jelly all that well).
                    – Kevin Cruijssen
                    7 mins ago











                    up vote
                    0
                    down vote














                    Python 3, 55 bytes





                    lambda s:not sum(int(i)*d for i,d in zip(s,[1,3]*7))%10


                    Try it online!






                    share|improve this answer
























                      up vote
                      0
                      down vote














                      Python 3, 55 bytes





                      lambda s:not sum(int(i)*d for i,d in zip(s,[1,3]*7))%10


                      Try it online!






                      share|improve this answer






















                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote










                        Python 3, 55 bytes





                        lambda s:not sum(int(i)*d for i,d in zip(s,[1,3]*7))%10


                        Try it online!






                        share|improve this answer













                        Python 3, 55 bytes





                        lambda s:not sum(int(i)*d for i,d in zip(s,[1,3]*7))%10


                        Try it online!







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 25 mins ago









                        HyperNeutrino

                        18.7k437147




                        18.7k437147




















                            up vote
                            0
                            down vote














                            Jelly, 14 bytes



                            D;0×1,3ẋ7¤S%⁵¬


                            Try it online!






                            share|improve this answer


























                              up vote
                              0
                              down vote














                              Jelly, 14 bytes



                              D;0×1,3ẋ7¤S%⁵¬


                              Try it online!






                              share|improve this answer
























                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote










                                Jelly, 14 bytes



                                D;0×1,3ẋ7¤S%⁵¬


                                Try it online!






                                share|improve this answer















                                Jelly, 14 bytes



                                D;0×1,3ẋ7¤S%⁵¬


                                Try it online!







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 5 mins ago

























                                answered 20 mins ago









                                HyperNeutrino

                                18.7k437147




                                18.7k437147




















                                    up vote
                                    0
                                    down vote














                                    Perl 6, 29 bytes





                                    :1[map 3×*+*,0,


                                    Try it online!





                                    share
























                                      up vote
                                      0
                                      down vote














                                      Perl 6, 29 bytes





                                      :1[map 3×*+*,0,


                                      Try it online!





                                      share






















                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote










                                        Perl 6, 29 bytes





                                        :1[map 3×*+*,0,


                                        Try it online!





                                        share













                                        Perl 6, 29 bytes





                                        :1[map 3×*+*,0,


                                        Try it online!






                                        share











                                        share


                                        share










                                        answered 1 min ago









                                        nwellnhof

                                        4,810920




                                        4,810920




















                                            up vote
                                            0
                                            down vote














                                            Japt -!, 13 bytes



                                            Ë*(Ev ª3Ãx %A


                                            Try it online!





                                            share


























                                              up vote
                                              0
                                              down vote














                                              Japt -!, 13 bytes



                                              Ë*(Ev ª3Ãx %A


                                              Try it online!





                                              share
























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote










                                                Japt -!, 13 bytes



                                                Ë*(Ev ª3Ãx %A


                                                Try it online!





                                                share















                                                Japt -!, 13 bytes



                                                Ë*(Ev ª3Ãx %A


                                                Try it online!






                                                share













                                                share


                                                share








                                                edited 7 secs ago

























                                                answered 8 mins ago









                                                Luis felipe De jesus Munoz

                                                3,34111049




                                                3,34111049



























                                                     

                                                    draft saved


                                                    draft discarded















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174495%2fvalidate-a-barcode%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest













































































                                                    Comments

                                                    Popular posts from this blog

                                                    What does second last employer means? [closed]

                                                    List of Gilmore Girls characters

                                                    Confectionery