Lazily Yield Numbers from Lines

Medium ⏱ 12 min 54% acceptance ★★★★★ 4.7
Write the generator numbers_in(lines) that takes any iterable of strings and yields each line converted to float — silently skipping lines that don't parse. Chain-friendly: feeding it a file object processes gigabytes lazily.

Examples

Example 1
Input
list(numbers_in(['3.5', 'oops', '7']))
Output
[3.5, 7.0]
Explanation

The bad line was skipped mid-stream.

Constraints

  • Must be a generator (yield).
  • Bad lines skipped via try/except.

Topics

Iterators & Generatorslazy pipeline

Companies

SplunkElasticCloudera

Hints

Hint 1

try: yield float(line.strip()) except ValueError: continue.

Hint 2

The caller controls consumption pace.

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