Skip to content

Instantly share code, notes, and snippets.

@raspi
Created May 20, 2015 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raspi/55c2b8d40b7e71ae43a6 to your computer and use it in GitHub Desktop.
Save raspi/55c2b8d40b7e71ae43a6 to your computer and use it in GitHub Desktop.
def is_vatnum(num):
if len(num) > 9:
return False
if "-" not in num:
return False
n,check = num.split("-")
try:
int(n)
except:
return False
try:
check = int(check)
except:
return False
multips = [7, 9, 10, 5, 8, 4, 2]
if len(n) != len(multips):
return False
control_sum = 0
for idx, mnum in enumerate(multips):
nnum = int(n[idx])
control_sum += nnum * mnum
control_sum %= 11
if control_sum == 0:
return check == control_sum
elif control_sum >= 2 and control_sum <= 10:
return (11 - control_sum) == check
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment