Sort by

recency

|

551 Discussions

|

  • + 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)
    
  • + 0 comments

    Here is HackerRank Validating UID in Python solution - https://programmingoneonone.com/hackerrank-validating-uid-solution-in-python.html

  • + 0 comments
    1. import re
      1. def check(uid):
    2. if len(uid)!=len(set(uid)):
    3. return "Invalid"
    4. else:
    5. match = re.match(r'^(?=(?:.[A-Z]){2,})(?=(?:.\d){3,})[A-Za-z0-9]{10}$',uid)
    6. if match:
    7. return "Valid"
    8. else:
    9. return "Invalid"
      1. for i in range(int(input())):
    10. print(check(input()))
  • + 1 comment
    def uid_validator(uid):
    	uid = uid.strip()
    	return (
    		'Valid' if (
    			re.fullmatch(r'^[0-9a-zA-Z]¨{10}', uid)
    			and len(re.findall(r'[A-Z], uid) >=2)
    			and len(re.findall(r'[0-9], uid) >= 3)
    			and len(set(uid) == 10
    		) else 'Invalid
    	)