Skip to content

Instantly share code, notes, and snippets.

@mattstibbs
Last active January 7, 2022 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattstibbs/cab5405197c094787a2b6fef8c1cb2cf to your computer and use it in GitHub Desktop.
Save mattstibbs/cab5405197c094787a2b6fef8c1cb2cf to your computer and use it in GitHub Desktop.
Validates a given NHS number to ensure it is valid according to the specification
"""
This code checks that an NHS number is of valid format based on the specified calculation and check digit.
It does NOT check that the NHS number is an active, valid NHS number according to the national NHS number database.
Requires: Python 3.6+
"""
# NHS numbers must be ten numerical digits long
nhs_number = "1354554531"
# The check digit is the last (10th) digit
check_digit = int(nhs_number[-1])
print(f"Check Digit: {check_digit}")
# The first 9 digits are used to validate against the check digit
digits_to_sum = nhs_number[0:9]
print(f"Digits To Sum: {digits_to_sum}")
# These multipliers are pre-defined, and relate to the position of the digit.
# e.g. the 1st position digit is multiplied by 10, the 2nd position digit is multiplied by 9 etc.
multipliers = {
1:10,
2:9,
3:8,
4:7,
5:6,
6:5,
7:4,
8:3,
9:2
}
total_sum = 0
# We work through the first 9 digits, multiplying each by their positional multiplier, and summing the result as we go
for idx, digit in enumerate(digits_to_sum):
# Gets the multiplier for this digit based on its position
current_multiplier = multipliers[idx + 1]
# Multiplies the digit by its positional multiplier, and adds the result to total_sum
total_sum = total_sum + (int(digit) * current_multiplier)
print(f"Index: {idx}, Digit: {int(digit)}, Multiplier: {current_multiplier}, Sum: {total_sum}")
# Once all digits have been multiplied and the sum calculated, we divide the total_sum by 11 and get the remainder
remainder = total_sum % 11
print(f"Remainder: {remainder}")
# We then subtract the remainder from 11 to give us the total
total = 11 - remainder
print(f"Total: {total}")
# The identifier is used to compare against the check digit we extracted earlier
# If the total is 11, we set the identifier to 0, otherwise we set the identifier to the total
if total == 11:
identifier = 0
else:
identifier = total
print(f"Identifier: {identifier}, Check Digit: {check_digit}")
# Finally, we check the identifier against the check digit to see if the NHS number is valid
if identifier == 10:
# If the identifier is 10, we know the NHS number is INVALID
print(f"{nhs_number} is an INVALID NHS number")
elif identifier == check_digit:
# If the identifier is equal to the check digit, we know the NHS number is VALID
print(f"{nhs_number} is a VALID NHS number")
else:
# If the identifier is any other value, we know the NHS number is INVALID
print(f"{nhs_number} is an INVALID NHS number")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment