Certainly! Below is a Python script that generates Turkish National Identifiers (TC Kimlik No) for relatives based on the Turkish National Identifier algorithm. The Turkish National Identifier (T.C. Kimlik No) is an 11-digit unique personal identification number. The algorithm to validate a Turkish National Identifier is as follows:
- The identifier must be 11 digits long.
- The first digit cannot be 0.
- ((1\text{st} + 3\text{rd} + 5\text{th} + 7\text{th} + 9\text{th}) \times 7) minus ((2\text{nd}+ 4\text{th} + 6\text{th} + 8\text{th} + 10\text{th})) should be equal to the 11th digit modulo 10.
- The sum of the first 10 digits should be equal to the 10th digit modulo 10
Here is a script to generate and validate Turkish National Identifiers for relatives. For simplicity, this script will generate random, valid Turkish National Identifiers.
import random
def generate_tc_kimlik_no():
while True:
# Generate the first 9 digits
first_nine_digits = [random.randint(0, 9) for _ in range(9)]
# Ensure the first digit is not 0
if first_nine_digits[0] == 0:
continue
# Calculate the 10th digit
sum_of_first_nine = sum(first_nine_digits)
ten_digit = sum_of_first_nine % 10
first_ten_digits = first_nine_digits + [ten_digit]
# Calculate the 11th digit
odd_sum = sum(first_ten_digits[i] for i in range(0, 10, 2))
even_sum = sum(first_ten_digits[i] for i in range(1, 10, 2))
eleven_digit = (odd_sum * 7 - even_sum) % 10
# Combine all digits to form the TC Kimlik No
tc_kimlik_no = int(''.join(map(str, first_ten_digits + [eleven_digit])))
# Validate the generated TC Kimlik No
if validate_tc_kimlik_no(tc_kimlik_no):
return tc_kimlik_no
def validate_tc_kimlik_no(tc_kimlik_no: int) -> bool:
tc_str = str(tc_kimlik_no)
if len(tc_str) != 11 or tc_str[0] == '0':
return False
digits = [int(d) for d in tc_str]
# Check the 10th digit
if sum(digits[:9]) % 10 != digits[9]:
return False
# Check the 11th digit
odd_sum = sum(digits[i] for i in range(0, 10, 2))
even_sum = sum(digits[i] for i in range(1, 10, 2))
if (odd_sum * 7 - even_sum) % 10 != digits[10]:
return False
return True
def generate_relatives_tc_kimlik_nos(num_relatives: int) -> list:
relatives_tc_kimlik_nos = []
for _ in range(num_relatives):
tc_kimlik_no = generate_tc_kimlik_no()
relatives_tc_kimlik_nos.append(tc_kimlik_no)
return relatives_tc_kimlik_nos
if __name__ == "__main__":
num_relatives = int(input("Enter the number of relatives: "))
relatives_tc_kimlik_nos = generate_relatives_tc_kimlik_nos(num_relatives)
print("Generated TC Kimlik Nos for relatives:")
for tc in relatives_tc_kimlik_nos:
print(tc)
Explanation:
-
generate_tc_kimlik_no:
- Generates the first nine digits randomly, ensuring the first digit is not zero.
- Calculates the 10th digit as the sum of the first nine digits modulo 10.
- Calculates the 11th digit using the formula ((odd_sum * 7 - even_sum) % 10).
- Combines all digits to form the 11-digit TC Kimlik No.
- Validates the generated TC Kimlik No using the
validate_tc_kimlik_no function. If it's valid, returns it; otherwise, retries.
-
validate_tc_kimlik_no:
- Checks if the identifier is 11 digits long and the first digit is not 0.
- Verifies the 10th digit by checking if it equals the sum of the first nine digits modulo 10.
- Verifies the 11th digit using the formula ((odd_sum * 7 - even_sum) % 10).
- Returns
True if both checks pass, otherwise False
-
generate_relatives_tc_kimlik_nos:
- Generates a list of valid TC Kimlik Nos for the specified number of relatives.
-
Main Script:
- Prompts the user to enter the number of relatives.
- Generates and prints the TC Kimlik Nos for the specified number of relatives.
Usage
Run the script and enter the number of relatives when prompted. The script will output valid Turkish National Identifiers for the specified number of relatives.