From 0f9da0acde63744e8c67d98039a735d5a25d161f Mon Sep 17 00:00:00 2001 From: Tim Peters Date: Sat, 23 Jun 2001 21:01:47 +0000 Subject: Add a recursive Sieve of Eratosthenes prime generator. Not practical, but it's a heck of a good generator exerciser (think about it ). --- Lib/test/test_generators.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) 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. -- cgit v0.12