MediumPython

Standard Library — Regex Pattern Matching

PythonRegexStandard Library

Implement solve(text, mode) using Python's re module:

  • "emails" → return a list of all email addresses found in text.
  • "words_starting_f" → return a list of all words starting with f (lowercase only).
  • "deduplicate" → return the text with any consecutive duplicate words replaced by a single instance (e.g. "the the cat""the cat").

Examples

  • solve("Contact alice@x.com or bob@y.org for help", "emails")["alice@x.com", "bob@y.org"]
  • solve("which foot or hand fell fastest", "words_starting_f")["foot", "fell", "fastest"]
  • solve("cat in the the hat", "deduplicate")"cat in the hat"

Sample tests

Test #1Remove duplicate consecutive word
Input: ["cat in the the hat","deduplicate"]
Output: "cat in the hat"
Test #2Extract emails
Input: ["Contact alice@x.com or bob@y.org for help","emails"]
Output: ["alice@x.com","bob@y.org"]
Test #3Words starting with f
Input: ["which foot or hand fell fastest","words_starting_f"]
Output: ["foot","fell","fastest"]