Sort by

recency

|

532 Discussions

|

  • + 0 comments

    test_cases = int(input()) for i in range(test_cases): string = input() upper_count=0 digits_count=0 for ele in string: if ele.isupper(): upper_count += 1 elif ele.isdigit(): digits_count += 1
    unique_count = len(string)==len(set(string)) if len(string)==10 and unique_count and string.isalnum() and upper_count >=2 and digits_count >= 3: print('Valid') else: print('Invalid')

    
    
  • + 1 comment
    from collections import Counter
    
    def validate(txt):
    
        #It must contain at least 2 uppercase English alphabet characters
        if sum([1 for x in txt if x.isupper()]) < 2:
            return False
    
        #It must contain at least 3 digits
        if sum([1 for x in txt if x.isdigit()]) < 3:
            return False
            
        #It should only contain alphanumeric characters 
        if not txt.isalnum():
            return False
    
        #No character should repeat.
        if (any(value != 1 for value in Counter(txt).values())):        
            return False
    
        #There must be exactly 10 characters in a valid UID.        
        if len(txt) != 10:
            return False
    
        
        return True
    
    n = int(input())
    for _ in range(n):
        txt = input().strip()
        print("Valid") if validate(txt) else  print("Invalid")
    
  • + 0 comments
    import re
    
    t = int(input())
    pattern = r'^(?=.*[A-Z]{2})(?=.*[0-9]{3})(?!.*(.)\1)[a-zA-Z0-9]{10}$'
    
    for _ in range(t):
        string = ''.join(sorted(input()))
        result = re.match(pattern, string)
        
        print('Valid' if result else 'Invalid')
    
  • + 0 comments

    ''' (?=(?:.\d.){3,}) => This is a lookahead to check that the string contains at least 3 digits (0-9). (?=(?:.[A-Z].){2,}) => This is a lookahead to check that the string contains at least 2 Uppercase letters (A-Z). (?!.(.).\1) => ensure that no character repeats. '^', '$' [a-zA-Z0-9]{10} => This part matches exactly 10 alphanumeric characters. ''' import re
    for _ in range(int(input())): print("Valid" if re.match(r"^(?=(?:.\d.){3,})(?=(?:.[A-Z].){2,})(?!.(.).\1)[a-zA-Z0-9]{10}$", input()) else "Invalid")

  • + 0 comments

    i tried to write the code in a clearer way, using re.VERBOSE, however i am unable to complete the excercise with this. in fact, test 3 fails expecting, for example, string "944A4NKtE2" to be valid (even though the character '4' is repeating). How so? is someone able to help me understand this?

    import re
    
    pattern = re.compile(r"""
        ^
        (?=.*[A-Z]{2,})     # At least 2 uppercase English alphabet characters
        (?=.*\d{3,})        # At least 3 digits
        (?=.*[a-zA-Z\d])    # Only alphanumeric characters
        (?!.*(.).*\1+.*)       # No repeated character
        .{10}
        $
        """, re.VERBOSE)
    
    for _ in range(int(input())):
        correspondence = pattern.search(input())
        if correspondence:
            print("Valid")
        else:
            print("Invalid")