filmov
tv
Unique Identification Number (UID) Validator – Python Regex Solution

Показать описание
ABCXYZ company assigns a Unique Identification Number (UID) to its employees. You have been tasked with validating these UIDs based on the following rules:
✅ Must contain at least 2 uppercase English alphabet characters (A-Z).
✅ Must contain at least 3 digits (0-9).
✅ Can only contain alphanumeric characters (A-Z, a-z, 0-9).
✅ No character should repeat within a UID.
✅ Must be exactly 10 characters in length.
Your task is to write a Python program that checks whether each UID provided in the input is Valid or Invalid based on these conditions.
Solution Approach:
Input Handling: The first line contains an integer N, which represents the number of test cases (UIDs). The next N lines contain a UID string.
Validation Checks Using Regular Expressions & Set Operations:
The UID should have exactly 10 characters (len(uid) == 10).
All characters must be unique, meaning len(set(uid)) == len(uid).
Output: For each UID, print "Valid" if all conditions are met; otherwise, print "Invalid".
Python Solution:
import re
def is_valid_uid(uid):
# Check if the UID has exactly 10 characters
if len(uid) != 10:
return "Invalid"
# Check if all characters are unique
if len(set(uid)) != len(uid):
return "Invalid"
# Check for at least 2 uppercase letters
return "Invalid"
# Check for at least 3 digits
return "Invalid"
# Ensure only alphanumeric characters are present
return "Invalid"
return "Valid"
def solve():
n = int(input().strip()) # Number of test cases
for _ in range(n):
uid = input().strip()
print(is_valid_uid(uid))
if __name__ == "__main__":
solve()
Sample Input & Output
2
B1CD102354
B1CDEF2354
Output
Invalid
Valid
Explanation:
"B1CD102354" is Invalid because '1' is repeated.
"B1CDEF2354" is Valid because it meets all the conditions.
✅ Must contain at least 2 uppercase English alphabet characters (A-Z).
✅ Must contain at least 3 digits (0-9).
✅ Can only contain alphanumeric characters (A-Z, a-z, 0-9).
✅ No character should repeat within a UID.
✅ Must be exactly 10 characters in length.
Your task is to write a Python program that checks whether each UID provided in the input is Valid or Invalid based on these conditions.
Solution Approach:
Input Handling: The first line contains an integer N, which represents the number of test cases (UIDs). The next N lines contain a UID string.
Validation Checks Using Regular Expressions & Set Operations:
The UID should have exactly 10 characters (len(uid) == 10).
All characters must be unique, meaning len(set(uid)) == len(uid).
Output: For each UID, print "Valid" if all conditions are met; otherwise, print "Invalid".
Python Solution:
import re
def is_valid_uid(uid):
# Check if the UID has exactly 10 characters
if len(uid) != 10:
return "Invalid"
# Check if all characters are unique
if len(set(uid)) != len(uid):
return "Invalid"
# Check for at least 2 uppercase letters
return "Invalid"
# Check for at least 3 digits
return "Invalid"
# Ensure only alphanumeric characters are present
return "Invalid"
return "Valid"
def solve():
n = int(input().strip()) # Number of test cases
for _ in range(n):
uid = input().strip()
print(is_valid_uid(uid))
if __name__ == "__main__":
solve()
Sample Input & Output
2
B1CD102354
B1CDEF2354
Output
Invalid
Valid
Explanation:
"B1CD102354" is Invalid because '1' is repeated.
"B1CDEF2354" is Valid because it meets all the conditions.