Skip to content

Commit 6cce942

Browse files
miss-islingtonpablogsal
authored andcommitted
Itertools sieve() recipe (GH-96813) (GH-96814)
1 parent 99f5568 commit 6cce942

File tree

1 file changed

+6
-32
lines changed

1 file changed

+6
-32
lines changed

Doc/library/itertools.rst

+6-32
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ loops that truncate the stream.
314314

315315
def count(start=0, step=1):
316316
# count(10) --> 10 11 12 13 14 ...
317-
# count(2.5, 0.5) -> 2.5 3.0 3.5 ...
317+
# count(2.5, 0.5) --> 2.5 3.0 3.5 ...
318318
n = start
319319
while True:
320320
yield n
@@ -739,7 +739,7 @@ which incur interpreter overhead.
739739

740740
def prepend(value, iterator):
741741
"Prepend a single value in front of an iterator"
742-
# prepend(1, [2, 3, 4]) -> 1 2 3 4
742+
# prepend(1, [2, 3, 4]) --> 1 2 3 4
743743
return chain([value], iterator)
744744

745745
def tabulate(function, start=0):
@@ -809,23 +809,13 @@ which incur interpreter overhead.
809809
for k in range(len(roots) + 1)
810810
]
811811

812-
def iter_index(seq, value, start=0):
813-
"Return indices where a value occurs in a sequence."
814-
# iter_index('AABCADEAF', 'A') --> 0 1 4 7
815-
i = start - 1
816-
try:
817-
while True:
818-
yield (i := seq.index(value, i+1))
819-
except ValueError:
820-
pass
821-
822812
def sieve(n):
823813
"Primes less than n"
824814
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
825815
data = bytearray([1]) * n
826816
data[:2] = 0, 0
827817
limit = math.isqrt(n) + 1
828-
for p in compress(range(limit), data):
818+
for p in compress(count(), islice(data, limit)):
829819
data[p+p : n : p] = bytearray(len(range(p+p, n, p)))
830820
return compress(count(), data)
831821

@@ -866,12 +856,12 @@ which incur interpreter overhead.
866856

867857
def triplewise(iterable):
868858
"Return overlapping triplets from an iterable"
869-
# triplewise('ABCDEFG') -> ABC BCD CDE DEF EFG
859+
# triplewise('ABCDEFG') --> ABC BCD CDE DEF EFG
870860
for (a, _), (b, c) in pairwise(pairwise(iterable)):
871861
yield a, b, c
872862

873863
def sliding_window(iterable, n):
874-
# sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
864+
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
875865
it = iter(iterable)
876866
window = collections.deque(islice(it, n), maxlen=n)
877867
if len(window) == n:
@@ -1103,6 +1093,7 @@ which incur interpreter overhead.
11031093
>>> import operator
11041094
>>> import collections
11051095
>>> import math
1096+
>>> import random
11061097

11071098
>>> take(10, count())
11081099
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -1152,7 +1143,6 @@ which incur interpreter overhead.
11521143
>>> list(repeatfunc(pow, 5, 2, 3))
11531144
[8, 8, 8, 8, 8]
11541145

1155-
>>> import random
11561146
>>> take(5, map(int, repeatfunc(random.random)))
11571147
[0, 0, 0, 0, 0]
11581148

@@ -1180,20 +1170,8 @@ which incur interpreter overhead.
11801170
>>> all(factored(x) == expanded(x) for x in range(-10, 11))
11811171
True
11821172

1183-
>>> list(iter_index('AABCADEAF', 'A'))
1184-
[0, 1, 4, 7]
1185-
>>> list(iter_index('AABCADEAF', 'B'))
1186-
[2]
1187-
>>> list(iter_index('AABCADEAF', 'X'))
1188-
[]
1189-
>>> list(iter_index('', 'X'))
1190-
[]
1191-
11921173
>>> list(sieve(30))
11931174
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
1194-
>>> small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59]
1195-
>>> all(list(sieve(n)) == [p for p in small_primes if p < n] for n in range(60))
1196-
True
11971175
>>> len(list(sieve(100)))
11981176
25
11991177
>>> len(list(sieve(1_000)))
@@ -1204,14 +1182,10 @@ which incur interpreter overhead.
12041182
9592
12051183
>>> len(list(sieve(1_000_000)))
12061184
78498
1207-
>>> carmichael = {561, 1105, 1729, 2465, 2821, 6601, 8911} # https://oeis.org/A002997
1208-
>>> set(sieve(10_000)).isdisjoint(carmichael)
1209-
True
12101185

12111186
>>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')]))
12121187
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
12131188

1214-
>>> import random
12151189
>>> random.seed(85753098575309)
12161190
>>> list(repeatfunc(random.random, 3))
12171191
[0.16370491282496968, 0.45889608687313455, 0.3747076837820118]

0 commit comments

Comments
 (0)