Validating Email

 

Validating Email Addresses With a Filter 


You are given an integer  followed by  email addresses. Your task is to print a list containing only valid email addresses in lexicographical order.

Valid email addresses must follow these rules:

  • It must have the username@websitename.extension format type.
  • The username can only contain letters, digits, dashes and underscores 
  • The website name can only have letters and digits 
  • The extension can only contain letters 
  • The maximum length of the extension is 

Concept

filter takes a function returning True or False and applies it to a sequence, returning a list of only those members of the sequence where the function returned True. A Lambdafunction can be used with filters. 

Let's say you have to make a list of the squares of integers from  to  (both included).

>> l = list(range(10))
>> l = list(map(lambda x:x*x, l))

Now, you only require those elements that are greater than  but less than .

>> l = list(filter(lambda x: x > 10 and x < 80, l))

Easy, isn't it?

Example

Complete the function fun in the editor below. 

fun has the following paramters: 

  • string s: the string to test 

Returns

  • boolean: whether the string is a valid email or not 

Input Format

The first line of input is the integer , the number of email addresses. 
 lines follow, each containing a string.

Constraints

Each line is a non-empty string.

Sample Input

3
lara@hackerrank.com
brian-23@hackerrank.com
britts_54@hackerrank.com

Sample Output

['brian-23@hackerrank.com', 'britts_54@hackerrank.com', 'lara@hackerrank.com']
CODE:#FEW TEST CASES FAILED
def fun(s):
    # return True if s is a valid email, else return False
    state=False
    
    if s.count('@')==1 and s.count('.')==1  and (s.index('@')<s.index('.')):
        a=s[:s.index('@')]
        b=s[s.index('@')+1:s.index('.')]
        c=s[s.index('.')+1:]
        if len(a)!=0 and len(b)!=0 and len(c)!=0:
            for i in a:
                state1=i in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_'
                if state1==False:
                    break
            state2=b.isalnum() 
            state3= len(c)==3 and c.isalpha() 
        state=state1 and state2 and state3
    return state
def filter_mail(emails):
    return list(filter(fun, emails))

if _name_ == '_main_':
    n = int(input())
    emails = []
    for _ in range(n):
        emails.append(input())

filtered_emails = filter_mail(emails)
filtered_emails.sort()
print(filtered_emails)

OTHER CODE:
def fun(email):
    
    #using try block
    try:
        
        # splitting the name and url of the email adress
        username, url = email.split('@')
        website, extension = url.split('.')
        
   # raise error if the email is not valied     
    except ValueError:
        return False

    # we are not replacing the - and _ 
    if username.replace('-', '').replace('_', '').isalnum() is False:
        return False
    
    # checking if all characters are alphabets and numerics
    elif website.isalnum() is False:
        return False
    
    # checking if the length is less than 3
    elif len(extension) > 3:
        return False
    else:
        return True

def filter_mail(emails):
    return list(filter(fun, emails))

if _name_ == '_main_':
    n = int(input())
    emails = []
    for _ in range(n):
        emails.append(input())

filtered_emails = filter_mail(emails)
filtered_emails.sort()
print(filtered_emails)

OTHER CODE:

Comments

Popular posts from this blog