Skip to content

Commit 210a5a9

Browse files
author
boraxpr
committed
bite 214
1 parent 098127c commit 210a5a9

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/153/README.md
77
/208/README.md
88
/176/README.md
9+
/214/README.md

214/countdown.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def countdown():
2+
"""Write a generator that counts from 100 to 1"""
3+
num = 100
4+
while True:
5+
yield num
6+
num -= 1
7+
if num < 1 or num > 100:
8+
raise StopIteration
9+
10+
11+
12+

214/test_countdown.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
3+
from itertools import islice
4+
5+
from countdown import countdown
6+
7+
8+
@pytest.fixture
9+
def cd():
10+
return countdown()
11+
12+
13+
def test_generator_values(cd):
14+
actual = list(islice(cd, 0, 100))
15+
expected = [100, 99, 98, 97, 96, 95, 94, 93, 92, 91,
16+
90, 89, 88, 87, 86, 85, 84, 83, 82, 81,
17+
80, 79, 78, 77, 76, 75, 74, 73, 72, 71,
18+
70, 69, 68, 67, 66, 65, 64, 63, 62, 61,
19+
60, 59, 58, 57, 56, 55, 54, 53, 52, 51,
20+
50, 49, 48, 47, 46, 45, 44, 43, 42, 41,
21+
40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
22+
30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
23+
20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
24+
10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
25+
assert actual == expected
26+
27+
28+
def test_going_beyond_one_hundred(cd):
29+
with pytest.raises(StopIteration):
30+
for _ in range(101):
31+
next(cd)
32+
33+

0 commit comments

Comments
 (0)