Hone logo
Hone
Problems

Find Users With Valid E-Mails

Many applications store user data, including email addresses. It's crucial to identify users with valid email formats to ensure effective communication and maintain data integrity. This challenge asks you to write a function that filters a list of user records, returning only those with email addresses that conform to a standard email format.

Problem Description

You are given a list of user records. Each user record is a dictionary containing at least a "name" (string) and an "email" (string) key. Your task is to write a function that takes this list of user records as input and returns a new list containing only the user records where the "email" value is a valid email address.

A valid email address is defined as follows:

  • It must contain exactly one "@" symbol.
  • The part before the "@" symbol (the "local part") must contain only alphanumeric characters (a-z, A-Z, 0-9) and periods (.).
  • The part after the "@" symbol (the "domain part") must contain at least one period (".") and end with a top-level domain (TLD) of at least two characters (e.g., "com", "org", "net").
  • The domain part cannot start or end with a period.

The function should be case-insensitive when validating the email address.

What needs to be achieved: Filter a list of user records to return only those with valid email addresses.

Key requirements: Implement a function that validates email addresses based on the defined rules and returns a filtered list of user records.

Expected behavior: The function should return a new list containing only the user records with valid email addresses. The original list should remain unchanged.

Edge cases to consider:

  • Empty input list.
  • User records without an "email" key.
  • Email addresses with multiple "@" symbols.
  • Email addresses with invalid characters in the local part.
  • Email addresses with invalid characters in the domain part.
  • Email addresses with missing periods in the domain part.
  • Email addresses with TLDs shorter than two characters.
  • Email addresses starting or ending with a period in the domain part.
  • Case sensitivity (should be ignored).

Examples

Example 1:

Input: [
    {"name": "Alice", "email": "alice.smith@example.com"},
    {"name": "Bob", "email": "bob.jones@sub.example.net"},
    {"name": "Charlie", "email": "charlie123@domain.co.uk"},
    {"name": "David", "email": "david@invalid"},
    {"name": "Eve", "email": "eve@.example.com"}
]
Output: [
    {"name": "Alice", "email": "alice.smith@example.com"},
    {"name": "Bob", "email": "bob.jones@sub.example.net"},
    {"name": "Charlie", "email": "charlie123@domain.co.uk"}
]
Explanation: Only Alice, Bob, and Charlie have valid email addresses according to the defined rules. David's email is invalid because it lacks a TLD, and Eve's email starts the domain with a period.

Example 2:

Input: []
Output: []
Explanation: An empty input list should return an empty list.

Example 3:

Input: [
    {"name": "Frank", "email": "frank@example.COM"},
    {"name": "Grace", "email": "grace@example.org"}
]
Output: [
    {"name": "Frank", "email": "frank@example.COM"},
    {"name": "Grace", "email": "grace@example.org"}
]
Explanation: Email validation should be case-insensitive.

Constraints

  • The input list will contain at most 1000 user records.
  • Each user record will be a dictionary.
  • The "name" value will be a string.
  • The "email" value will be a string.
  • The function should execute in O(n) time complexity, where n is the number of user records.
  • The function should not modify the original input list.

Notes

Consider using regular expressions for email validation, but ensure your regex is efficient and accurately reflects the defined rules. Alternatively, you can implement the validation logic using string manipulation functions. Remember to handle edge cases carefully to ensure the function's robustness. Focus on clarity and readability in your code. Think about how to break down the validation process into smaller, manageable steps.

Loading editor...
plaintext