-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_email_addresses.py
More file actions
64 lines (41 loc) · 1.84 KB
/
validate_email_addresses.py
File metadata and controls
64 lines (41 loc) · 1.84 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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 but less than .
# >> l = list(filter(lambda x: x > 10 and x < 80, l))
# Easy, isn't it?
# 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.
# 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']
# Problem's link: https://www.hackerrank.com/challenges/validate-list-of-email-address-with-filter #
import re
n = int(raw_input().strip())
addresses = []
for i in range(n):
address.append(raw_input())
addresses.sort()
validate = lambda address: bool(re.match(r'^[A-Za-z0-9_-]+@[A-Za-z0-9]+\.[\w.-]{1,3}$', address))
res = filter(validate, addresses)
print res