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, info, which contains a six-digit manufacturer identification number followed by a single space and a comma-delimited list of item numbers, determine UPC codes for all of the items. Sort the UPC codes in ascending order and return them in a String[].
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, info, which contains a six-digit manufacturer identification number followed by a single space and a comma-delimited list of item numbers, determine UPC codes for all of the items. Sort the UPC codes in ascending order and return them in a String[].
Definition
- Class:
- UPCGenerator
- Method:
- generate
- Parameters:
- String
- Returns:
- String[]
- Method signature:
- String[] generate(String info)
- (be sure your method is public)
Constraints
- info will be formatted as "<manufacturer> <item>,<item>,<item>,..." (quotes for clarity only).
- <manufacturer> will be a six-digit number.
- Each <item> will be a five-digit number.
- info will contain between 1 and 7 <item>s, inclusive.
- info will contain no duplicate <item>s.
Examples
"731453 09592"
Returns: { "731453095922" }The UPC code from the problem statement."731453 09592,08272,08832"
Returns: { "731453082724", "731453088320", "731453095922" }Three items from manufacturer 731453. The UPC codes in the return String[] are sorted in ascending order."042282 41462,41552,41502,27702,00412,69162"
Returns: { "042282004123", "042282277022", "042282414625", "042282415028", "042282415523", "042282691620" }