Skip to content

Instantly share code, notes, and snippets.

@ansakoy
Created February 6, 2016 21:41
Show Gist options
  • Save ansakoy/370981bb4324cef3dcb5 to your computer and use it in GitHub Desktop.
Save ansakoy/370981bb4324cef3dcb5 to your computer and use it in GitHub Desktop.
Скрипт для определения валидности ИНН
# -*- coding: utf-8 -*-
def inn_is_valid(target_inn):
# Проверяет валидность ИНН по контрольному числу
# Возвращает True в случае валидности и False в обратном случае
length = len(target_inn)
base = target_inn[:-1]
if length == 10:
factors_10 = [2, 4, 10, 3, 5, 9, 4, 6, 8, 0]
sum_products = 0
for digit in xrange(len(base)):
sum_products += int(base[digit]) * factors_10[digit]
result = (sum_products % 11) % 10
if result > 9:
key = result % 10
else:
key = result
if key == int(target_inn[-1]):
return True
else:
return False
elif length == 12:
factors_12_1 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0]
factors_12_2 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0]
sum_products1 = 0
for digit in xrange(len(base)):
sum_products1 += int(base[digit]) * factors_12_1[digit]
result1 = sum_products1 % 11
if result1 > 9:
key_1 = result1 % 10
else:
key_1 = result1
sum_products2 = 0
for digit in xrange(length):
sum_products2 += int(target_inn[digit]) * factors_12_2[digit]
result2 = sum_products2 % 11
if result2 > 9:
key_2 = result2 % 10
else:
key_2 = result2
if key_1 == int(target_inn[10]) and key_2 == int(target_inn[11]):
return True
else:
return False
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment