Shelf label audit
EasyOnline Assessment
Audit the barcode labels scanned from warehouse shelves and report the ones that cannot be right.
A valid label is exactly 8 characters long and made only of the digits 0 to 9. Its last digit is a check digit for the first seven. Multiply the first seven digits by the weights 7, 3, 1, 7, 3, 1, 7 in that order (first digit by 7, second by 3, third by 1, and so on) and add the products to get total. The check digit must be the single digit that makes total + check a multiple of 10; so when total is already a multiple of 10 the check digit is 0.
A label that has any other length, or contains any character that is not a digit (letters and spaces included), is invalid, and so is a label whose check digit does not match.
Input: labels, a list of strings.
Output: a list of integers, the indices of all invalid labels in increasing order. If every label is valid (or labels is empty), return [].
Example 1
- Input:
- [["12345676","12345675","31415927"]]
- Output:
- [1]
- Explanation
- For
12345676the first seven digits weighted 7, 3, 1, 7, 3, 1, 7 give 7 + 6 + 3 + 28 + 15 + 6 + 49 = 114, so the check digit must be (10 - 4) % 10 = 6, which matches.12345675ends in 5 instead, so label 1 is invalid.31415927has weighted sum 73 and check digit 7, so it is valid.
Example 2
- Input:
- [["00000000","0000000A","1234567","12345678"]]
- Output:
- [1,2,3]
- Explanation
00000000has weighted sum 0, so its check digit is 0 (not 10) and the label is valid.0000000Acontains a letter,1234567has only 7 characters, and12345678has a wrong check digit (it should be 6), so labels 1, 2 and 3 are invalid.
Example 3
- Input:
- [["10000003","50000005","99999999"]]
- Output:
- []
- Explanation
10000003has weighted sum 7 and check digit 3,50000005has sum 35 and check digit 5, and99999999has sum 261 and check digit 9. All three match, so nothing is invalid.
Constraints
0 <= labels.length <= 2,0000 <= labels[i].length <= 12; characters are printable ASCII (letters, digits, spaces, punctuation)