Problem Statement
A UPC code is a 12-digit number comprised of three parts. The first six digits represent the manufacturer identification number, the next five digits represent the item number, and the last digit is known as the check digit. When a UPC code is read by a scanner, the check digit is used to determine whether or not it was scanned correctly. Here's how the check digit is calculated:
1. Sum all of the digits that are in odd positions (the first digit is considered to be in position 1).
2. Multiply this number by 3.
3. Sum all of the digits that are in even positions except for the check digit.
4. Add this number to the result of step 2.
5. Determine the smallest non-negative number that, when added to the result of step 4, would result in a multiple of 10. This number is the check digit. For example, consider a UPC code whose first 11 digits are 73145309592. The sum of all the digits in odd positions is 7 + 1 + 5 + 0 + 5 + 2 = 20. The sum of all the digits in even positions is 3 + 4 + 3 + 9 + 9 = 28. Adding this number to 3 times the first result is 28 + 3 * 20 = 88. The smallest non-negative number that can be added to 88 to turn it into a multiple of 10 is 2. Therefore, the check digit is 2 and the complete 12-digit UPC code would be 731453095922. Given a String[], codes, each element of which is a scanned 12-digit UPC code, determine which codes were scanned incorrectly. Return a String[] containing exactly the same number of elements as codes. The ith element of the return String[] should be "invalid" if the ith element of codes has an invalid check digit, and "valid" otherwise. Both "invalid" and "valid" must be completely lowercase.
2. Multiply this number by 3.
3. Sum all of the digits that are in even positions except for the check digit.
4. Add this number to the result of step 2.
5. Determine the smallest non-negative number that, when added to the result of step 4, would result in a multiple of 10. This number is the check digit. For example, consider a UPC code whose first 11 digits are 73145309592. The sum of all the digits in odd positions is 7 + 1 + 5 + 0 + 5 + 2 = 20. The sum of all the digits in even positions is 3 + 4 + 3 + 9 + 9 = 28. Adding this number to 3 times the first result is 28 + 3 * 20 = 88. The smallest non-negative number that can be added to 88 to turn it into a multiple of 10 is 2. Therefore, the check digit is 2 and the complete 12-digit UPC code would be 731453095922. Given a String[], codes, each element of which is a scanned 12-digit UPC code, determine which codes were scanned incorrectly. Return a String[] containing exactly the same number of elements as codes. The ith element of the return String[] should be "invalid" if the ith element of codes has an invalid check digit, and "valid" otherwise. Both "invalid" and "valid" must be completely lowercase.
Definition
- Class:
- CodeScanner
- Method:
- success
- Parameters:
- String[]
- Returns:
- String[]
- Method signature:
- String[] success(String[] codes)
- (be sure your method is public)
Constraints
- codes will contain between 1 and 50 elements, inclusive.
- Each element of codes will contain exactly 12 characters.
- Each element of codes will contain only digits ('0'-'9').
Examples
{"731453095922"}Returns: { "valid" }The UPC code from the problem statement. It is scanned correctly.{"731453095923"}Returns: { "invalid" }The check digit does not agree with the first eleven digits.{"731453056220", "078635369723", "731453088321"}Returns: { "valid", "invalid", "invalid" }Only the first UPC code was scanned correctly.