• + 0 comments

    I've had too many regex solutions in a row, so I decided to go a different way for this. I tried to focus on readability (and debugability).

    import collections
    
    # 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 all( [c.isalnum() for c in uid] ):
            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 max(collections.Counter(uid).values()) > 1:
            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)