diff --git a/Week05/emails_mustafafurkan_yilmaz.py b/Week05/emails_mustafafurkan_yilmaz.py new file mode 100644 index 00000000..cfc7f8bf --- /dev/null +++ b/Week05/emails_mustafafurkan_yilmaz.py @@ -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__()