We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Regex and Parsing
- Validating UID
- Discussions
Validating UID
Validating UID
Sort by
recency
|
561 Discussions
|
Please Login in order to post a comment
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 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 returnsTrue
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 returnTrue
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.Of course, the solution to this simple problem is a one-liner (not counting
import
s)…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…
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.
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]
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')