Header Ad

HackerRank Validating UID solution in python

In this Validating UID 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.

HackerRank Validating UID solution in python


Problem solution in Python 2 programming.

import re
for i in range(int(raw_input())):
    N = raw_input().strip()
    if N.isalnum() and len(N) == 10:
        if bool(re.search(r'(.*[A-Z]){2,}',N)) and bool(re.search(r'(.*[0-9]){3,}',N)):
            if re.search(r'.*(.).*\1+.*',N):
                print 'Invalid'
            else:
                print 'Valid'    
        else:
            print 'Invalid'
    else:
        print 'Invalid'



Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import re

for _ in range(int(input())):
    u = ''.join(sorted(input()))
    try:
        assert re.search(r'[A-Z]{2}', u)
        assert re.search(r'\d\d\d', u)
        assert not re.search(r'[^a-zA-Z0-9]', u)
        assert not re.search(r'(.)\1', u)
        assert len(u) == 10
    except:
        print('Invalid')
    else:
        print('Valid')


Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT

ids = []
line_num = int(raw_input())
while line_num:
  ids.append(raw_input());
  line_num -= 1;

import re

for id in ids:
  chars_count = {char:id.count(char) for char in id}
  upper = 0
  not_alnum = 0;
  dig = 0;
  for ch in id:
    if ch.isupper(): upper += 1
    if not ch.isalnum(): not_alnum = 1;
    if ch.isdigit(): dig += 1;

  if len(id) != 10: print 'Invalid'
  elif len(set(chars_count.values())) != 1: print 'Invalid'
  elif upper < 2: print 'Invalid'
  elif dig < 3: print 'Invalid'
  elif not_alnum: print 'Invalid'
  else: print 'Valid'


Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import re

def valid_uid(uid):
    if len(re.findall(r"[A-Z]", uid)) < 2 or \
            len(re.findall(r"[0-9]", uid)) < 3 or \
            not re.match(r"[A-Za-z0-9]{10}$", uid) or \
            len(set(uid)) != len(uid):
        return False
    
    return True
    
n = int(input())
for _ in range(n):
    uid = input()
    if valid_uid(uid):
        print("Valid")
    else:
        print("Invalid")


Post a Comment

0 Comments