-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path03-generator-pipeline.py
More file actions
69 lines (49 loc) · 2.17 KB
/
03-generator-pipeline.py
File metadata and controls
69 lines (49 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Challenge: Generator Pipeline
Difficulty: Intermediate
Concepts: generators, yield, chaining, lazy evaluation
Time: 30 minutes
Build a data processing pipeline using generators. Implement three generator
functions that can be chained together:
1. `read_lines(text)` -- yields each line from a multi-line string
2. `filter_nonempty(lines)` -- yields only non-empty, non-whitespace lines
3. `to_upper(lines)` -- yields each line converted to uppercase
Then implement `pipeline(text)` that chains all three and returns a list.
Examples:
>>> pipeline("hello\\n\\nworld\\n \\nfoo")
['HELLO', 'WORLD', 'FOO']
"""
def read_lines(text: str):
"""Yield each line from a multi-line string. Implement this generator."""
# Hint: Use text.split("\\n") and yield each element.
pass
def filter_nonempty(lines):
"""Yield only lines that are not empty or whitespace-only. Implement this generator."""
# Hint: Use .strip() to check if a line has content.
pass
def to_upper(lines):
"""Yield each line converted to uppercase. Implement this generator."""
# Hint: Use .upper() on each line.
pass
def pipeline(text: str) -> list[str]:
"""Chain the three generators and return the result as a list. Implement this function."""
# Hint: Pass the output of one generator as input to the next.
pass
# --- Tests (do not modify) ---
if __name__ == "__main__":
# Test 1: Basic pipeline
assert pipeline("hello\n\nworld\n \nfoo") == ["HELLO", "WORLD", "FOO"], "Basic pipeline failed"
# Test 2: All empty lines
assert pipeline("\n\n \n") == [], "All empty lines failed"
# Test 3: Single line
assert pipeline("test") == ["TEST"], "Single line failed"
# Test 4: read_lines is a generator
gen = read_lines("a\nb")
assert hasattr(gen, "__next__"), "read_lines should be a generator"
# Test 5: Generators are lazy (filter_nonempty)
gen = filter_nonempty(iter(["hello", "", "world"]))
assert next(gen) == "hello", "filter_nonempty laziness failed"
assert next(gen) == "world", "filter_nonempty second yield failed"
# Test 6: Empty input
assert pipeline("") == [], "Empty input failed"
print("All tests passed!")