Validate a barcode
Clash 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):
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 = 102Then, 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 = 29Add the numbers obtained in steps 1 and 2 together:
29 + 102 = 131
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:
- Receive a barcode as its input.
- Check whether the barcode is valid
- 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
 |Â
show 6 more comments
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):
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 = 102Then, 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 = 29Add the numbers obtained in steps 1 and 2 together:
29 + 102 = 131
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:
- Receive a barcode as its input.
- Check whether the barcode is valid
- 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
why did the last digit (9
) become a4
– 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
 |Â
show 6 more comments
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):
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 = 102Then, 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 = 29Add the numbers obtained in steps 1 and 2 together:
29 + 102 = 131
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:
- Receive a barcode as its input.
- Check whether the barcode is valid
- 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
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):
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 = 102Then, 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 = 29Add the numbers obtained in steps 1 and 2 together:
29 + 102 = 131
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:
- Receive a barcode as its input.
- Check whether the barcode is valid
- 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
code-golf arithmetic
edited 21 mins ago
asked 34 mins ago


Wais Kamal
1375
1375
why did the last digit (9
) become a4
– 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
 |Â
show 6 more comments
why did the last digit (9
) become a4
– 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
 |Â
show 6 more comments
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)
add a comment |Â
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
Nice answer! Definitely shorter than what I was working on. I like thex
you've used in combination withO
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 whereS13S13âˆÂ*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
add a comment |Â
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!
add a comment |Â
up vote
0
down vote
Jelly, 14 bytes
D;0×1,3ẋ7¤S%⵬
Try it online!
add a comment |Â
up vote
0
down vote
Perl 6, 29 bytes
:1[map 3×*+*,0,
Try it online!
add a comment |Â
up vote
0
down vote
Japt -!
, 13 bytes
Ë*(Ev ª3Ãx %A
Try it online!
add a comment |Â
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)
add a comment |Â
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)
add a comment |Â
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)
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)
edited 11 mins ago
answered 25 mins ago
G B
7,2471327
7,2471327
add a comment |Â
add a comment |Â
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
Nice answer! Definitely shorter than what I was working on. I like thex
you've used in combination withO
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 whereS13S13âˆÂ*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
add a comment |Â
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
Nice answer! Definitely shorter than what I was working on. I like thex
you've used in combination withO
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 whereS13S13âˆÂ*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
add a comment |Â
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
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
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 thex
you've used in combination withO
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 whereS13S13âˆÂ*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
add a comment |Â
Nice answer! Definitely shorter than what I was working on. I like thex
you've used in combination withO
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 whereS13S13âˆÂ*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
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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!
Python 3, 55 bytes
lambda s:not sum(int(i)*d for i,d in zip(s,[1,3]*7))%10
Try it online!
answered 25 mins ago


HyperNeutrino
18.7k437147
18.7k437147
add a comment |Â
add a comment |Â
up vote
0
down vote
Jelly, 14 bytes
D;0×1,3ẋ7¤S%⵬
Try it online!
add a comment |Â
up vote
0
down vote
Jelly, 14 bytes
D;0×1,3ẋ7¤S%⵬
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Jelly, 14 bytes
D;0×1,3ẋ7¤S%⵬
Try it online!
Jelly, 14 bytes
D;0×1,3ẋ7¤S%⵬
Try it online!
edited 5 mins ago
answered 20 mins ago


HyperNeutrino
18.7k437147
18.7k437147
add a comment |Â
add a comment |Â
up vote
0
down vote
Perl 6, 29 bytes
:1[map 3×*+*,0,
Try it online!
add a comment |Â
up vote
0
down vote
Perl 6, 29 bytes
:1[map 3×*+*,0,
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Perl 6, 29 bytes
:1[map 3×*+*,0,
Try it online!
Perl 6, 29 bytes
:1[map 3×*+*,0,
Try it online!
answered 1 min ago
nwellnhof
4,810920
4,810920
add a comment |Â
add a comment |Â
up vote
0
down vote
Japt -!
, 13 bytes
Ë*(Ev ª3Ãx %A
Try it online!
add a comment |Â
up vote
0
down vote
Japt -!
, 13 bytes
Ë*(Ev ª3Ãx %A
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Japt -!
, 13 bytes
Ë*(Ev ª3Ãx %A
Try it online!
Japt -!
, 13 bytes
Ë*(Ev ª3Ãx %A
Try it online!
edited 7 secs ago
answered 8 mins ago


Luis felipe De jesus Munoz
3,34111049
3,34111049
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
why did the last digit (
9
) become a4
– 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