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.
I have used regex to solve this problem, and some python logics.
I have created 3 differents patterns (1 for each condition, except the last one which check at the same time if the UID is exactly 10 characters).
first condition pattern check if 2 uppercase (at least) are present or not.
Note : ?. is a lookaround operators and it means anything before an uppercase. So "....A....".
This format must be repeated at least twice : {2,}
Exact same logics with digits 0-9
And the last pattern, I think, is logic to understand, we only want a-zA-Z0-9 characters exactly 10 times.
Then, we just have to check condition by condition.
Here my code, I am pretty sure there is a simple way or more concise way to solve this problem, but, it works and the logics sounds good :
# Enter your code here. Read input from STDIN. Print output to STDOUTimportreT=int(input())UID_list=list()for_inrange(T):current_UID=input()UID_list.append(current_UID)# Patternsfirst_condition_pattern=r'^(?=(?:.*[A-Z]){2,}).*$'second_condition_pattern=r"^(?=(?:.*[0-9]){3,}).*$"third_condition_pattern=r"[a-zA-Z0-9]{10}"forelementinUID_list:iflen(set(element))!=len(list(element)):#print("Repeat character")print("Invalid")elifbool(re.match(first_condition_pattern,element))==False:#print("Not 2 upper case minimum")print("Invalid")elifbool(re.match(second_condition_pattern,element))==False:#print("Not 3 digits minimum")print("Invalid")elifbool(re.match(third_condition_pattern,element))==False:#print("Not only alphanumeric character or not exactly 10 characters")print("Invalid")else:print("Valid")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Validating UID
You are viewing a single comment's thread. Return to all comments →
I have used regex to solve this problem, and some python logics.
I have created 3 differents patterns (1 for each condition, except the last one which check at the same time if the UID is exactly 10 characters).
first condition pattern check if 2 uppercase (at least) are present or not.
Note : ?. is a lookaround operators and it means anything before an uppercase. So "....A....". This format must be repeated at least twice : {2,}
Exact same logics with digits 0-9
And the last pattern, I think, is logic to understand, we only want a-zA-Z0-9 characters exactly 10 times.
Then, we just have to check condition by condition.
Here my code, I am pretty sure there is a simple way or more concise way to solve this problem, but, it works and the logics sounds good :