Engineering Numbers: Looking for a check code

63 Views Asked by At

Not sure if this is the best place to ask but I own an engineering company.

We have expanded and I need a way to ensure part numbers are not entered incorrectly.

Part numbers are of the form:

$$Aaaxxx-xxxx-c$$

Ignore the the dash '-' characters, they are just there to make things more readable and are not stored in the database.

'A' is a single character [A..Z] or one of '!', '~', '#' or '$'
'a' is a single character [A..Z],
'x' is a digit [0..9]

I have yet to define 'c' this is intended to be a check character to ensure no bad entries.

Typical typing errors are:

  • Swapping two characters

  • Entering the wrong one

I am looking for suggestions on where I can look to find an algorithm to define 'c'.

I am not worried about trying to correct an error only to recognise it.

1

There are 1 best solutions below

3
On BEST ANSWER

In Python you can use the following code to calculate the last symbol for an arbitrary input string. The function is based on built-in MD5 function. MD5 function is very sensitive to input - the output changes drasticly for minor input changes. The function is slightly adjusted to ignore dashes and spaces in the input string.

import hashlib

def calculateLast(s):
    # set of symbols from which the last symbol will be picked
    # feel free to change it 
    checkSymbol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    s1 = s.replace('-', '').replace(' ', '').encode()
    h = hashlib.md5(s1).hexdigest()
    num = int(h, 16)
    index = num % len(checkSymbol)
    return checkSymbol[index]    

print(calculateLast("Axz - 243 - 5847"))

MD5 is not the fastest way to compute a hash (on the contrary). But on modern computers it's not at issue. And you will find MD5 implementations in all modern programming languages.