Question: Validating Email Addresses With a Filter - Hacker Rank (Python Functionals)
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 [a – z], [A – Z], [0 – 9], [_-].
- The website name can only have letters and digits [a – z], [A – Z], [0 – 9].
- The extension can only contain letters [a – z], [A – Z].
- 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))
Example:
Complete the function fun in the editor below.
fun has the following parameters:
- 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 N, the number of email addresses.
N lines follow, each containing a string.
Constraints:
Each line is a non-empty string.
Input sample:
3
lara@hackerrank.com
brian-23@hackerrank.com
britts_54@hackerrank.com
Output sample:
['brian-23@hackerrank.com', 'britts_54@hackerrank.com', 'lara@hackerrank.com']
Possible solutions
We will now use multiple ways to solve this question. The following Python code is already given in the Hacker Rank.
def fun(s):
# return True if s is a valid email, else return False
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)
So, we have to build the fun()
method. We will use the following ways to solve the question.
- try and except blocks
- using
re.compile
method - using
re.match
method
Solution-1: Using try and except blocks
We will now use the try and except block to check if the given email address is valid or not and then change accordingly. The try block lets you test a block of code for errors. The except block lets you handle the error. Let us start with the coding part.
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)
Input:
3
lara@hackerrank.com
brian-23@hackerrank.com
britts_54@hackerrank.com
Output:
['brian-23@hackerrank.com', 'britts_54@hackerrank.com', 'lara@hackerrank.com']
As you can see, the email addresses were verified by Python code.
Solution-2: Using re.compile method
We will now use the regular expressions to validate the email addresses. A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing). We will use the compile() method, which can combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search for a pattern again without rewriting it.
This solution requires you to have a better understanding of regualar expressions. Let us start the coding.
# importing the regular expression module
import re
def fun(s):
# defining the patterns
pattern = re.compile("^[\\w-]+@[0-9a-zA-Z]+\\.[a-z]{1,3}$")
# returing if the email is valid
return pattern.match(s)
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)
Input:
3
lara@hackerrank.com
brian-23@hackerrank.com
britts_54@hackerrank.com
Output:
['brian-23@hackerrank.com', 'britts_54@hackerrank.com', 'lara@hackerrank.com']
As you can see, the emails were verified using regular expressions.
Solution-3: Using re.match method
We will now use the match() to validate the email addresses. The match method returns a corresponding match object instance if zero or more characters at the beginning of the string match the regular expression pattern. In simple words, the re.match() returns a match object only if the pattern is located at the beginning of the string; otherwise, it will return None.
Let us start the coding
# importing the module
import re
def fun(s):
# using re.match function
a = re.match(r'[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\.[a-zA-Z]{1,3}$',s)
#returning the email adress
return(a)
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)
Input:
3
lara@hackerrank.com
brian-23@hackerrank.com
britts_54@hackerrank.com
Output:
['brian-23@hackerrank.com', 'britts_54@hackerrank.com', 'lara@hackerrank.com']
As you can see, we get the desired output:
Summary
In this short tutorial, we learned about how we can validate email addresses in Python based on the conditions given in the question. We come up with three different solutions to solve the question.
References
Python re
Question on Hacker Rank: Validating Email Addresses With a Filter
Related Keywords: hackerrank valid email address regex, validating email addresses with a filter hackerrank solution, valid email address hackerrank, detect the email addresses hackerrank solution, valid email address hackerrank solution in python, python functional hacker rank solution