@@ -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):
@@ -812,6 +812,16 @@ which incur interpreter overhead.
812812 for k in range(len(roots) + 1)
813813 ]
814814
815+ def sieve(n):
816+ "Primes less than n"
817+ # sieve(30) --> 2 3 5 7 11 13 17 19 23 29
818+ data = bytearray([1]) * n
819+ data[:2] = 0, 0
820+ limit = math.isqrt(n) + 1
821+ for p in compress(count(), islice(data, limit)):
822+ data[p+p : n : p] = bytearray(len(range(p+p, n, p)))
823+ return compress(count(), data)
824+
815825 def flatten(list_of_lists):
816826 "Flatten one level of nesting"
817827 return chain.from_iterable(list_of_lists)
@@ -842,12 +852,12 @@ which incur interpreter overhead.
842852
843853 def triplewise(iterable):
844854 "Return overlapping triplets from an iterable"
845- # triplewise('ABCDEFG') -> ABC BCD CDE DEF EFG
855+ # triplewise('ABCDEFG') -- > ABC BCD CDE DEF EFG
846856 for (a, _), (b, c) in pairwise(pairwise(iterable)):
847857 yield a, b, c
848858
849859 def sliding_window(iterable, n):
850- # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
860+ # sliding_window('ABCDEFG', 4) -- > ABCD BCDE CDEF DEFG
851861 it = iter(iterable)
852862 window = collections.deque(islice(it, n), maxlen=n)
853863 if len(window) == n:
@@ -1079,6 +1089,7 @@ which incur interpreter overhead.
10791089 >>> import operator
10801090 >>> import collections
10811091 >>> import math
1092+ >>> import random
10821093
10831094 >>> take(10 , count())
10841095 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -1128,7 +1139,6 @@ which incur interpreter overhead.
11281139 >>> list (repeatfunc(pow , 5 , 2 , 3 ))
11291140 [8, 8, 8, 8, 8]
11301141
1131- >>> import random
11321142 >>> take(5 , map (int , repeatfunc(random.random)))
11331143 [0, 0, 0, 0, 0]
11341144
@@ -1156,10 +1166,22 @@ which incur interpreter overhead.
11561166 >>> all (factored(x) == expanded(x) for x in range (- 10 , 11 ))
11571167 True
11581168
1169+ >>> list (sieve(30 ))
1170+ [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
1171+ >>> len (list (sieve(100 )))
1172+ 25
1173+ >>> len (list (sieve(1_000 )))
1174+ 168
1175+ >>> len (list (sieve(10_000 )))
1176+ 1229
1177+ >>> len (list (sieve(100_000 )))
1178+ 9592
1179+ >>> len (list (sieve(1_000_000 )))
1180+ 78498
1181+
11591182 >>> list (flatten([(' a' , ' b' ), (), (' c' , ' d' , ' e' ), (' f' ,), (' g' , ' h' , ' i' )]))
11601183 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
11611184
1162- >>> import random
11631185 >>> random.seed(85753098575309 )
11641186 >>> list (repeatfunc(random.random, 3 ))
11651187 [0.16370491282496968, 0.45889608687313455, 0.3747076837820118]
0 commit comments