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 the percentage of codes that were scanned incorrectly according to their check digits. Return this value as a String formatted as "XXX.XX%" where each X is a single digit and there are exactly three digits to the left of the decimal point and exactly two digits to the right of the decimal point. Add leading zeros when necessary and truncate digits which do not appear in the return String (no rounding).
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 the percentage of codes that were scanned incorrectly according to their check digits. Return this value as a String formatted as "XXX.XX%" where each X is a single digit and there are exactly three digits to the left of the decimal point and exactly two digits to the right of the decimal point. Add leading zeros when necessary and truncate digits which do not appear in the return String (no rounding).
Definition
- Class:
- ScannerReliability
- Method:
- accuracy
- Parameters:
- String[]
- Returns:
- String
- Method signature:
- String accuracy(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: "100.00%"
The UPC code from the problem statement. It is scanned correctly.{"731453095923"}Returns: "000.00%"
{"731453056220", "078635369724", "731453088321"}Returns: "066.66%"
The first two codes are scanned correctly.