In this Validating Email Addresses with a filter problem, You are given an integer N followed by N email addresses. Your task is to print a list containing only valid email addresses in lexicographical order.


HackerRank Validating Email Addresses with a Filter solution in python


Problem solution in Python 2 programming.

import re

n = int(raw_input())
ans = []
for i in range(n):
  s = raw_input()
  
  match = re.search(r'^[\w\d-]+\@[a-zA-Z0-9]+\.\w?\w?\w?$',s)
  if match:
    ans.append(s)
  
ans.sort()
print ans


Problem solution in Python 3 programming.

def fun(email):
    try:
        username, url = email.split("@")
        website, extension = url.split(".")
    except ValueError:
        return False
    
    if username.replace("-", "").replace("_", "").isalnum() is False:
        return False
    elif website.isalnum() is False:
        return False
    elif len(extension) > 3:
        return False
    else:
        return True


Problem solution in pypy programming.

import re

def fun(string):
	if string.count("@") == 1 and string.count('.') ==1:
		esplit = string.split('@')
		user =esplit[0]
		web = esplit[1].split('.')[0]
		ext = esplit[1].split('.')[1]
		return all([check_user(user),check_web(web),check_ext(ext)])
	else:
		return False


def check_user(strg, search=re.compile(r'[^a-zA-Z0-9-_]').search):
	lenght = True if len(strg)>0 else False
	return all([lenght,not(bool(search(strg)))])

def check_web(strg,search=re.compile(r'[^a-zA-Z0-9]').search):
	lenght = True if len(strg)>0 else False
	return all([lenght,not(bool(search(strg)))])

def check_ext(strg):
	lenght = False
	if len(strg) <= 3 and len(strg)>0:
		lenght = True
	return lenght


Problem solution in pypy3 programming.

import re
def fun(s):
    # return True if s is a valid email, else return False
    '''
    n=int(input())
    e=[]
    for _ in range[n]:
        e.append(input())
    user= e.split("@")[0]
    print(user)
    '''
    '''
    l = list(map(lambda x:x.split("@")[0], s))
    l = list(filter(lambda x: x > 10 and x < 80, l))
    '''
    #print(s)
    ptrn = re.compile("^[a-zA-Z][\w-]*@[a-zA-Z0-9]+\.[a-zA-Z]{1,3}$")
    result = ptrn.match(s)
    #l = list(filter(lambda x:x.match(ptrn) , s))
    return result