forked from minaevd/hackerrank-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_email.py
executable file
·49 lines (38 loc) · 1.33 KB
/
validate_email.py
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
"""
Title: Validating Email Addresses With a Filter
Description: 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 [email protected] 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.
"""
import re
def validate_email(N, emails):
l = []
for email in emails:
email_splitted = email.split('@')
if(len(email_splitted) == 2):
username = email_splitted[0]
website = email_splitted[1]
else:
continue
website_splitted = website.split('.')
if(len(website_splitted) == 2):
websitename = website_splitted[0]
extension = website_splitted[1]
else:
continue
if(
re.match("^[a-zA-Z0-9-_]+$", username, re.I) and
re.match("^[a-zA-Z0-9]+$", websitename, re.I) and
len(extension) <= 3
):
l.append(email)
return sorted(l)
N = int(raw_input().strip())
emails = []
for i in range(N):
emails.append(str(raw_input().strip()))
print validate_email(N, emails)