• + 0 comments

    After reviewing the disdcussions here, I realized the code I just posted should have the following improvements:

    • Don't use a counter to detect character repeats, set is much better
    • isalnum() works on string, don't go character by character
    • In this case, generater expressions are better than list comprehensions

    Here is the updated code:

    # A valid UID must:
    #
    #   * Be exactly 10 characters in length
    #   * Only contain alphanumeric characters (a-z, A-Z, 0-9)
    #   * Contain at least 2 uppercase English alphabet characters
    #   * Contain at least 3 digits (0-9)
    #   * Not repeat any character
    
    def validate_uid(uid):
        if len(uid) != 10:
            return False
    
        if not uid.isalnum():
            return False
    
        if sum(c.isupper() for c in uid) < 2:
            return False
    
        if sum(c.isdigit() for c in uid) < 3:
            return False
    
        if len(set(uid)) != len(uid):
            return False
    
        return True
    
    t = int(input())
    
    for _ in range(t):
        uid = input()
        is_valid = validate_uid(uid)
    
        message = 'Valid' if is_valid else 'Invalid'
        print(message)