Skip to content

Instantly share code, notes, and snippets.

@amirshnll
Created April 15, 2024 22:55
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 amirshnll/05c5dad77628281c717af523388a4ff3 to your computer and use it in GitHub Desktop.
Save amirshnll/05c5dad77628281c717af523388a4ff3 to your computer and use it in GitHub Desktop.
validate iranian national code (pytohn)
def validate_iranian_national_code(code):
code_len = len(code)
if code_len > 10 or code_len < 8:
return False
if len(set(code)) == 1:
return False
if len(code) < 10:
code = code.zfill(10)
factors = [10, 9, 8, 7, 6, 5, 4, 3, 2]
checksum = sum(int(code[i]) * factors[i] for i in range(len(code) - 1))
remainder = checksum % 11
last_digit = int(code[-1])
if remainder < 2:
return remainder == last_digit
else:
return 11 - remainder == last_digit
# Test cases
print(validate_iranian_national_code(code="5998613771"))
print(validate_iranian_national_code(code="4398013083"))
print(validate_iranian_national_code(code="0000000000"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment