From 9bc96b71b1f8d0680d273f78ae74f21b88c2363f Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 14 Oct 2022 20:58:23 -0500 Subject: [PATCH 1/4] Reduce the range of needed updates --- Doc/library/itertools.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 6571114ef311f9..49d1897c1c51c4 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -816,7 +816,7 @@ which incur interpreter overhead. data[:2] = 0, 0 limit = math.isqrt(n) + 1 for p in compress(range(limit), data): - data[p+p : n : p] = bytearray(len(range(p+p, n, p))) + data[p*p : n : p] = bytearray(len(range(p*p, n, p))) return compress(count(), data) def flatten(list_of_lists): From d8191a35c15936d927a01af84dc46d3f38365017 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 14 Oct 2022 21:04:29 -0500 Subject: [PATCH 2/4] Fix indentation --- Doc/library/itertools.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 49d1897c1c51c4..fc02b1e77df78b 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -810,14 +810,14 @@ which incur interpreter overhead. ] 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 p in compress(range(limit), data): - data[p*p : n : p] = bytearray(len(range(p*p, n, p))) - return compress(count(), data) + "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 p in compress(range(limit), data): + data[p*p : n : p] = bytearray(len(range(p*p, n, p))) + return compress(count(), data) def flatten(list_of_lists): "Flatten one level of nesting" From 1b3361fd58647f4eee48ba94721a3c40bb821b20 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 15 Oct 2022 10:52:58 -0500 Subject: [PATCH 3/4] Speed-up sieve() recipe with iter_index() --- Doc/library/itertools.rst | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index fc02b1e77df78b..32d11d997b86b0 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -809,6 +809,16 @@ which incur interpreter overhead. for k in range(len(roots) + 1) ] + def iter_index(seq, value, start=0): + "Return indices where a value occurs in a sequence." + # iter_index('AABCADEAF', 'A') --> 0 1 4 7 + i = start - 1 + try: + while True: + yield (i := seq.index(value, i+1)) + except ValueError: + pass + def sieve(n): "Primes less than n" # sieve(30) --> 2 3 5 7 11 13 17 19 23 29 @@ -817,7 +827,7 @@ which incur interpreter overhead. limit = math.isqrt(n) + 1 for p in compress(range(limit), data): data[p*p : n : p] = bytearray(len(range(p*p, n, p))) - return compress(count(), data) + return iter_index(data, 1) def flatten(list_of_lists): "Flatten one level of nesting" From 5c5825f1de6e8a209bc78141ab0138a17ebaf147 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 15 Oct 2022 11:35:43 -0500 Subject: [PATCH 4/4] Add tests for iter_index() --- Doc/library/itertools.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 32d11d997b86b0..056b0788a4d846 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -1180,6 +1180,15 @@ which incur interpreter overhead. >>> all(factored(x) == expanded(x) for x in range(-10, 11)) True + >>> list(iter_index('AABCADEAF', 'A')) + [0, 1, 4, 7] + >>> list(iter_index('AABCADEAF', 'B')) + [2] + >>> list(iter_index('AABCADEAF', 'X')) + [] + >>> list(iter_index('', 'X')) + [] + >>> list(sieve(30)) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] >>> small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]