Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Task UPC-A Code Validation
12-09-2009, 11:30 AM
Post: #1
Task UPC-A Code Validation
I cannot solve this myself just yet but i am trying. This is a sample question for the computer bowl

1. Create a program that will validate a UPC-A codes by ensuring the correct Check digit . The program should only accept a twelve digit number. The output should indicate of the number is valid or invalid. If the Check Digit is invalid for a 12 digit code the correct check sum digit should also be displayed in the output.

Step 1
Sum all of the digits in the odd positions together.
0+5+7+1+4+2 = 19(A)
0+4+0+1+5+9 = 19(B)
Step 2
Multiply the sum from Step 1 by 3
3*19 = 57 ( A & B )
Step 3
Sum of all the digits in the even positions together ( not including the digit check)
7+6+8+6+1 = 28(A)
6+2+0+1+8 = 17(B)
Step 4
Sum together the results from step 2 and 3
28 + 57 = 85 (A)
17 + 57 = 74 (B)
Step 5 Subtract the sum from the next highest multiple of 10
90 - 85 = 5 [check digit] (A)
80-74 = 6 [check digit] (B)

Sample Run
Input: Enter in UPC-A Code: 075678164125
Output: UPC Code is correct
Visit this user's website Find all posts by this user
Quote this message in a reply
12-09-2009, 03:39 PM (This post was last modified: 12-09-2009 03:46 PM by codecaine.)
Post: #2
RE: Task UPC-A Code Validation
what is the digit check in step 3? this problem should be easy to do. Just need to understand exactly what they want. Python will murder this challenge lol.

import sys;

numstr = ""
numlist = ""
numstr = raw_input("Enter in UPC-A Code: ");
#check if its 12 numbers long and is a string with just digits
if (len(numstr)) <> 12 or numstr.isdigit == False:
print "invalid UPC-A code"
sys.exit(1);

#convert numlist to a array of integers
numlist = map(int,numstr)

Check if first 6 digits sum is equal to the second half 6 digit sum
if sum(numlist[0:6]) == sum(numlist[6:]):
print "invalid UPC-A code"
sys.exit(1);

#step 2 multiply sum
msum = sum(numlist[0:6]) * 3
I just did this in the interpreter. When I check each step is working correctly ill would make a file and make the code elegant etc... The interpreter allows you to see result of each line of code your doing. Smile
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: