Sort by

recency

|

554 Discussions

|

  • + 0 comments
    T= int(input())
    
    for _ in range(T):
        criteria = []
        user_id_str= input()
        user_id = [i for i in user_id_str]
        
        #1. At least 2 uppercase English alphabet 
        criteria.append((len([element.isupper() for element in user_id if element.isupper() is True])) >= 2)
        
        #2. At least 3 digits
        criteria.append((len([element.isdigit() for element in user_id if element.isdigit() is True])) >= 3)   
           
        #3. only contain alphanumeric
        criteria.append(user_id_str.isalnum())
        
        #4. No character should repeat
        criteria.append((len(user_id)) == (len(set(user_id))))
        
        #5. Must be exactly 10 characters 
        criteria.append(len(user_id) == 10)
        
        print("Valid" if all(criteria) else "Invalid")
       
            
            
    
  • + 0 comments

    import re

    uid = list T = int(input()) pattern = r'^(?=(?:.[A-Z]){2,})(?=(?:.\d){3,})[A-Za-z\d]+$'

    for _ in range(T): # print(T) id = input() if len(set(id))==10: if re.match(pattern, id): print("Valid") else: print("Invalid") else: print("Invalid")

  • + 0 comments
    import re
    n= int(input())
    valido =True
         
    for _ in range(n):
        UID =input();
        if len(UID)==10:
            valido = True
        else:
            valido =False
        
        if valido  and UID.isalnum():
            valido =True
        else:
            valido = False
        if valido and len(re.findall(r'[A-Z]',UID))>=2:
            valido = True
        else:
            valido =False
        if valido and len(re.findall(r'[0-9]',UID))>=3:
            valido = True
        else:
            valido =False
        
        for c in UID:
            if len(re.findall(c,UID))!=1:
                valido=False
                break
            
            
        print("Valid" if valido else "Invalid")
        
        
    
  • + 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)
    
  • + 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)