From 26943e7d37c0a67b1ca43cda838b4741951b7809 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 13 Sep 2022 16:00:17 -0500 Subject: [PATCH 1/3] Add sieve() recipe to demonstrate count, islice, and compress --- Doc/library/itertools.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index c35ff8c4343fc9..8c809dd10bb8eb 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -812,6 +812,16 @@ which incur interpreter overhead. for k in range(len(roots) + 1) ] + def sieve(n): + "Primes less than n" + # sieve(30) --> 2 3 5 7 11 13 17 19 23 29 + data = bytearray([1]) * n + data[:2] = 0, 0 + limit = math.isqrt(n) + 1 + for c in compress(count(), islice(data, limit)): + data[c+c::c] = bytearray(len(range(c+c, n, c))) + return compress(count(), data) + def flatten(list_of_lists): "Flatten one level of nesting" return chain.from_iterable(list_of_lists) @@ -1156,6 +1166,19 @@ which incur interpreter overhead. >>> all(factored(x) == expanded(x) for x in range(-10, 11)) True + >>> list(sieve(30)) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] + >>> len(list(sieve(100))) + 25 + >>> len(list(sieve(1_000))) + 168 + >>> len(list(sieve(10_000))) + 1229 + >>> len(list(sieve(100_000))) + 9592 + >>> len(list(sieve(1_000_000))) + 78498 + >>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')])) ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] From 56699758bb2033cdd6f107827a8a737b8f3d5c0c Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 13 Sep 2022 16:02:39 -0500 Subject: [PATCH 2/3] Group imports together and make arrow notation consistent --- Doc/library/itertools.rst | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 8c809dd10bb8eb..f1c394285703d7 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -314,7 +314,7 @@ loops that truncate the stream. def count(start=0, step=1): # count(10) --> 10 11 12 13 14 ... - # count(2.5, 0.5) -> 2.5 3.0 3.5 ... + # count(2.5, 0.5) --> 2.5 3.0 3.5 ... n = start while True: yield n @@ -739,7 +739,7 @@ which incur interpreter overhead. def prepend(value, iterator): "Prepend a single value in front of an iterator" - # prepend(1, [2, 3, 4]) -> 1 2 3 4 + # prepend(1, [2, 3, 4]) --> 1 2 3 4 return chain([value], iterator) def tabulate(function, start=0): @@ -852,12 +852,12 @@ which incur interpreter overhead. def triplewise(iterable): "Return overlapping triplets from an iterable" - # triplewise('ABCDEFG') -> ABC BCD CDE DEF EFG + # triplewise('ABCDEFG') --> ABC BCD CDE DEF EFG for (a, _), (b, c) in pairwise(pairwise(iterable)): yield a, b, c def sliding_window(iterable, n): - # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG + # sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG it = iter(iterable) window = collections.deque(islice(it, n), maxlen=n) if len(window) == n: @@ -1089,6 +1089,7 @@ which incur interpreter overhead. >>> import operator >>> import collections >>> import math + >>> import random >>> take(10, count()) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] @@ -1138,7 +1139,6 @@ which incur interpreter overhead. >>> list(repeatfunc(pow, 5, 2, 3)) [8, 8, 8, 8, 8] - >>> import random >>> take(5, map(int, repeatfunc(random.random))) [0, 0, 0, 0, 0] @@ -1182,7 +1182,6 @@ which incur interpreter overhead. >>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')])) ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] - >>> import random >>> random.seed(85753098575309) >>> list(repeatfunc(random.random, 3)) [0.16370491282496968, 0.45889608687313455, 0.3747076837820118] From 17d2266833f959ed26c3ceb4a9478bd955aa719a Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 13 Sep 2022 17:03:30 -0500 Subject: [PATCH 3/3] Change variable to "p" for "prime" --- Doc/library/itertools.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index f1c394285703d7..9a6e061fa0f2e6 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -818,8 +818,8 @@ which incur interpreter overhead. data = bytearray([1]) * n data[:2] = 0, 0 limit = math.isqrt(n) + 1 - for c in compress(count(), islice(data, limit)): - data[c+c::c] = bytearray(len(range(c+c, n, c))) + for p in compress(count(), islice(data, limit)): + data[p+p : n : p] = bytearray(len(range(p+p, n, p))) return compress(count(), data) def flatten(list_of_lists):