Sort by

recency

|

561 Discussions

|

  • + 0 comments

    import sys import string

    upper = set(string.ascii_uppercase) digit = set(string.digits) alnum = set(string.ascii_letters) | digit

    N = int(input()) uids = map(lambda u: set(u.strip()), sys.stdin.readlines()[:N]) for u in uids: if (len(u & alnum) == 10 and len(u & upper) >= 2 and len(u & digit) >= 3): print('Valid') else: print('Invalid')

  • + 0 comments

    I notice that a lot of solutions, including the editorial, use isalnum(). If it works with this problem, I guess that's great, but I think a lot of people forget that function matches a lot of non-ASCII characters.

    Python's isalnum() method works with Unicode characters. It returns True if all characters in the string are alphanumeric, meaning they are either letters or numbers according to the Unicode standard. This includes characters from various languages and scripts beyond just the basic Latin alphabet and Arabic numerals.

    For example, isalnum() would return True for strings containing characters like '½' (vulgar fraction one half), '౩' (Telugu digit three), superscript digits, or subscript digits as these are recognized as alphanumeric within the Unicode character set.

  • + 0 comments

    Of course, the solution to this simple problem is a one-liner (not counting imports)…

    import string, sys
    print(*list(map(lambda u: 'Valid' if (len((u := set(u.strip())) & alnum) == 10 and len(u & upper) >= 2 and len(u & digit) >= 3) else 'Invalid', [(upper := set(string.ascii_uppercase)), (digit := set(string.digits)), (alnum := set(string.ascii_letters) | digit), (N := int(input())), sys.stdin.readlines()[:N]][-1])), sep='\n')
    

    Just kidding. Yes, it works, but I derived the one-liner from my initial solution. My solution uses simple sets, so it's easier to understand than regular expressions and much, much faster…

    import sys
    import string
    
    upper = set(string.ascii_uppercase)
    digit = set(string.digits)
    alnum = set(string.ascii_letters) | digit
    
    N = int(input())
    uids = map(lambda u: set(u.strip()), sys.stdin.readlines()[:N])
    for u in uids:
        if (len(u & alnum) == 10 and
            len(u & upper) >= 2 and
            len(u & digit) >= 3):
            print('Valid')
        else:
            print('Invalid')
    

    I think the omission of regex from my solution is OK. This section of challenges is titled "Regex and Parsing". It parses the input, so it's a legitimate solution.

  • + 0 comments
    import re
    for _ in range(int(input())):
        inp=input()
        isvalid="Invalid"
        if (re.fullmatch(r'[0-9a-zA-z]{10}',inp) 
        and len(set(inp))==10 
        and sum('0'<=c<='9' for c in inp)>2 
        and sum('A'<=c<='Z' for c in inp)>1):
                isvalid="Valid"
        print(isvalid)
    

    why not isdigit() or isalnum()? They allow unicode characters (decimal digits in other language locales) and superscripts. our goal for digits is to only have [0-9]

  • + 1 comment

    from collections import Counter import re n = int(input()) ls = [] for i in range(n): a = input() ls.append(a)

    for i in ls: c = Counter(i) if len(i) <= 10: if any(val > 1 for key , val in c.items()): print('Invalid') else: upper = len(re.findall(r'[A-Z]',i)) digit = len(re.findall(r'[0-9]',i)) if upper >= 2 and digit >= 3: print('Valid') else: print('Invalid')

    else:
        print('Invalid')