Validating Email Addresses With a Filter in Python - HackerRank Solution
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.
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 maximum length of the extension is 3.
Concept :
A 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 Lambda function can be used with filters.
Let's say you have to make a list of the squares of integers from 0 to 9(both
included).
>> l = list(range(10)) >> l = list(map(lambda x:x*x, l))
Now, you only require those elements that are greater than 10 but less than
80.
>> l = list(filter(lambda x: x > 10 and x < 80, l))
Easy, isn't it?
Input Format :
N lines follow, each containing a string.
Constraints :
Each line is a non-empty string.
Output Format :
Output a list containing the valid email addresses in lexicographical order.
If the list is empty, just output an empty list, [].
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']
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # Validating Email Addresses With a Filter in Python - Hacker Rank Solution # Python 3 # Validating Email Addresses With a Filter in Python - Hacker Rank Solution START def fun(s): # return True if s is a valid email, else return False try: username, url = s.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 # Validating Email Addresses With a Filter in Python - Hacker Rank Solution END 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) |
Disclaimer :-
the above whole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.
Nice Comment...
Laminar Air Flow
filter manufacturers in chennai