Created
April 15, 2024 22:55
-
-
Save amirshnll/05c5dad77628281c717af523388a4ff3 to your computer and use it in GitHub Desktop.
validate iranian national code (pytohn)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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