Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Week05/emails_mustafafurkan_yilmaz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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__()