-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathemails_mustafafurkan_yilmaz.py
More file actions
43 lines (35 loc) · 1.6 KB
/
emails_mustafafurkan_yilmaz.py
File metadata and controls
43 lines (35 loc) · 1.6 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
import re
class Emails(list):
def __init__(self, emails):
# 1. Validate that all inputs are strings
if not all(isinstance(email, str) for email in emails):
raise ValueError("All items must be strings.")
# 2. Validate email format using Regex
# Pattern checks for: non-empty-chars @ non-empty-chars . non-empty-chars
pattern = re.compile(r"^[^@]+@[^@]+\.[^@]+$")
for email in emails:
if not pattern.match(email):
raise ValueError(f"Invalid email address: {email}")
# 3. Remove duplicates while preserving order
# dict.fromkeys() is a fast way to remove duplicates in Python 3.7+
unique_emails = list(dict.fromkeys(emails))
# Initialize the list with the processed data
super().__init__(unique_emails)
# 4. Create a .data attribute to satisfy test_validate_duplicates
# Since this class inherits from list, 'self' holds the data.
# We point .data to self to allow .data.count() to work as expected by the test.
self.data = self
def validate(self):
"""
Existence of this method is required by test_validate.
Actual validation logic is handled in __init__ to ensure
ValueErrors are raised during instantiation.
"""
return True
def __repr__(self):
# Returns a string representation that can reproduce the object
# e.g., Emails(['a@b.com'])
return f"Emails({super().__repr__()})"
def __str__(self):
# String representation of the list
return super().__repr__()