33
1
An EAN-8 barcode includes 7 digits of information and an 8th checksum digit.
The checksum is calculated by multiplying the digits by 3 and 1 alternately, adding the results, and subtracting from the next multiple of 10.
For example, given the digits 2103498
:
Digit: 2 1 0 3 4 9 8
Multiplier: 3 1 3 1 3 1 3
Result: 6 1 0 3 12 9 24
The sum of these resulting digits is 55, so the checksum digit is 60 - 55 = 5
The Challenge
Your task is to, given an 8 digit barcode, verify if it is valid - returning a truthy value if the checksum is valid, and falsy otherwise.
- You may take input in any of the following forms:
- A string, 8 characters in length, representing the barcode digits
- A list of 8 integers, the barcode's digits
- A non-negative integer (you can either assume leading zeroes where none are given, i.e.
1
=00000001
, or request input with the zeroes given)
- Builtins that compute the EAN-8 checksum (i.e, take the first 7 digits and calculate the last) are banned.
- This is code-golf, so the shortest program (in bytes) wins!
Test Cases
20378240 -> True
33765129 -> True
77234575 -> True
00000000 -> True
21034984 -> False
69165430 -> False
11965421 -> False
12345678 -> False
Related to Luhn algorithm for verifying credit card numbers, possibly a dupe.
– xnor – 2017-11-15T17:58:44.4901This question is not actually about a bar code (which is the black-white striped thing), but about the number encoded by a barcode. The number can exist without a bar code, and the bar code can encode other things than EANs. Maybe just "Is my EAN-8 valid" is a better title? – Paŭlo Ebermann – 2017-11-15T19:49:22.687
2@PaŭloEbermann doesn't quite have the same ring to it... – FlipTack – 2017-11-15T20:17:02.160
7When reading about barcodes, I expect some image reading (or at least a bit-string), not verifying a checksum. – Paŭlo Ebermann – 2017-11-15T20:19:40.747
Strongly related, since an ISBN-13 is an EAN. – Olivier Grégoire – 2017-11-16T14:55:56.637