Validating UID HackerRank Solution In Python

Problem

ABCXYZ company has up to 100 employees.
The company decides to create a unique identification number (UID) for each of its employees.
The company has assigned you the task of validating all the randomly generated UIDs.

A valid UID must follow the rules below:

  • It must contain at least 2 uppercase English alphabet characters.
  • It must contain at least 3 digits (0 – 9).
  • It should only contain alphanumeric characters (a – z, A – Z & 0 -9 ).
  • No character should repeat.
  • There must be exactly 10 characters in a valid UID.

Input Format

The first line contains an integer T, the number of test cases.
The next T lines contains an employee’s UID.

Output Format

For each test case, print ‘Valid’ if the UID is valid. Otherwise, print ‘Invalid’, on separate lines. Do not print the quotation marks.

Sample Input

2B1CD102354B1CDEF2354

Sample Output

InvalidValid

Explanation

B1CD1023541 is repeating → Invalid
B1CDEF2354: Valid

Solution – Validating UID Solution In Python | HackerRank

import refor _ in range(int(input())):    lst = []    s = input()    for i in [r"[A-Z0-9]{10}", r"([A-Z].*){2,}", r"([0-9].*){3,}"]:        lst.append(bool(re.search(i, s, flags=re.I)))    if all(lst) is True:        if bool(re.search(r".*(.).*\1.*", s)) is True:            print("Invalid")        else:            print("Valid")    else:        print("Invalid")

Explanation – Validating UID Solution In Python | HackerRank

I shall be try to make the likely unclear part of this solution very clear.

for _ in range(int(input())):    lst = []    s = input()    for i in [r"[A-Z0-9]{10}", r"([A-Z].*){2,}", r"([0-9].*){3,}"]:        lst.append(bool(re.search(i, s, flags=re.I)))

The object(lst) created is serving the purpose of storing my boolean responses after making an individual search of each of the given conditions in the given strings.

 [r"[A-Z0-9]{10}", r"([A-Z].*){2,}", r"([0-9].*){3,}"]

While i have created the above list in other to keep the code neat, it consists some of the condition given. Then the last line search for the characters that match the regex pattern in the list and stores the boolean values in a new list.

if all(lst) is True:

The above if condition checks if all the conditions stated in the previous list are true or not. If they are true, a check of the last condition is gonna come in. which is to check whether the given string contains more than one of the same character or not then print out the right output.

 if all(lst) is True:        if bool(re.search(r".*(.).*\1.*", s)) is True:            print("Invalid")        else:            print("Valid")    else:        print("Invalid")

Comments