Compile Once, Scan Many

Medium ⏱ 12 min 55% acceptance ★★★★☆ 4.4
Write error_codes(lines) that scans thousands of log lines for codes like E-1234 — compiling the pattern once with re.compile outside the loop, then using the compiled object's .search per line. Return the list of codes found (first per line).

Examples

Example 1
Input
error_codes(['boot ok', 'fail E-1042 retry', 'crash E-9 E-77'])
Output
['E-1042', 'E-9']
Explanation

One code per matching line, first occurrence.

Constraints

  • re.compile exactly once.
  • Pattern: E-\d+.

Topics

Regular Expressionsre.compile

Companies

CloudflareAkamaiDatadog

Hints

Hint 1

Module-level or function-top compiled object.

Hint 2

m.group(0) when search hits.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.