summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-06-23 21:01:47 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-06-23 21:01:47 (GMT)
commit0f9da0acde63744e8c67d98039a735d5a25d161f (patch)
treead3958bc562d120d6163451523a023a020892a4d
parent6ba5f79674bded76c1d8011fbbb9b5e6caa5afb5 (diff)
downloadcpython-0f9da0acde63744e8c67d98039a735d5a25d161f.zip
cpython-0f9da0acde63744e8c67d98039a735d5a25d161f.tar.gz
cpython-0f9da0acde63744e8c67d98039a735d5a25d161f.tar.bz2
Add a recursive Sieve of Eratosthenes prime generator. Not practical,
but it's a heck of a good generator exerciser (think about it <wink>).
-rw-r--r--Lib/test/test_generators.py40
1 files changed, 39 insertions, 1 deletions
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index 9889588..947697a 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -303,9 +303,47 @@ in try/except, not like a return.
[1, 2, 3]
"""
+# Fun tests (for sufficiently warped notions of "fun").
+
+fun_tests = """
+
+Build up to a recursive Sieve of Eratosthenes generator.
+
+>>> def firstn(g, n):
+... return [g.next() for i in range(n)]
+
+>>> def intsfrom(i):
+... while 1:
+... yield i
+... i += 1
+
+>>> firstn(intsfrom(5), 7)
+[5, 6, 7, 8, 9, 10, 11]
+
+>>> def exclude_multiples(n, ints):
+... for i in ints:
+... if i % n:
+... yield i
+
+>>> firstn(exclude_multiples(3, intsfrom(1)), 6)
+[1, 2, 4, 5, 7, 8]
+
+>>> def sieve(ints):
+... prime = ints.next()
+... yield prime
+... not_divisible_by_prime = exclude_multiples(prime, ints)
+... for p in sieve(not_divisible_by_prime):
+... yield p
+
+>>> primes = sieve(intsfrom(2))
+>>> firstn(primes, 20)
+[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]
+"""
+
__test__ = {"tut": tutorial_tests,
"pep": pep_tests,
- "zemail": email_tests}
+ "email": email_tests,
+ "fun": fun_tests}
# Magic test name that regrtest.py invokes *after* importing this module.
# This worms around a bootstrap problem.