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, code, which contains 11 of the 12 digits of a UPC code, determine the value of the missing digit. The position of the missing digit in the code will be denoted as an asterisk ('*'). Return the missing digit as an int.
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, code, which contains 11 of the 12 digits of a UPC code, determine the value of the missing digit. The position of the missing digit in the code will be denoted as an asterisk ('*'). Return the missing digit as an int.
Definition
- Class:
- IncompleteUPC
- Method:
- digits
- Parameters:
- String
- Returns:
- int
- Method signature:
- int digits(String code)
- (be sure your method is public)
Constraints
- code will contain exactly 12 characters.
- code will contain only digits ('0'-'9') and a single asterisk ('*').
Examples
"73145309592*"
Returns: 2
This is the UPC code from the problem statement. The missing digit is the check digit."7314*3095922"
Returns: 5
The same code with a different digit missing."73145305622*"
Returns: 0
The missing check digit is 0."0*8635369724"
Returns: 7