@@ -314,7 +314,7 @@ loops that truncate the stream.
314
314
315
315
def count(start=0, step=1):
316
316
# 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 ...
318
318
n = start
319
319
while True:
320
320
yield n
@@ -739,7 +739,7 @@ which incur interpreter overhead.
739
739
740
740
def prepend(value, iterator):
741
741
"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
743
743
return chain([value], iterator)
744
744
745
745
def tabulate(function, start=0):
@@ -812,6 +812,16 @@ which incur interpreter overhead.
812
812
for k in range(len(roots) + 1)
813
813
]
814
814
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(range(limit), data):
822
+ data[p+p : n : p] = bytearray(len(range(p+p, n, p)))
823
+ return compress(count(), data)
824
+
815
825
def flatten(list_of_lists):
816
826
"Flatten one level of nesting"
817
827
return chain.from_iterable(list_of_lists)
@@ -842,12 +852,12 @@ which incur interpreter overhead.
842
852
843
853
def triplewise(iterable):
844
854
"Return overlapping triplets from an iterable"
845
- # triplewise('ABCDEFG') -> ABC BCD CDE DEF EFG
855
+ # triplewise('ABCDEFG') -- > ABC BCD CDE DEF EFG
846
856
for (a, _), (b, c) in pairwise(pairwise(iterable)):
847
857
yield a, b, c
848
858
849
859
def sliding_window(iterable, n):
850
- # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
860
+ # sliding_window('ABCDEFG', 4) -- > ABCD BCDE CDEF DEFG
851
861
it = iter(iterable)
852
862
window = collections.deque(islice(it, n), maxlen=n)
853
863
if len(window) == n:
@@ -1079,6 +1089,7 @@ which incur interpreter overhead.
1079
1089
>>> import operator
1080
1090
>>> import collections
1081
1091
>>> import math
1092
+ >>> import random
1082
1093
1083
1094
>>> take(10 , count())
1084
1095
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -1128,7 +1139,6 @@ which incur interpreter overhead.
1128
1139
>>> list (repeatfunc(pow , 5 , 2 , 3 ))
1129
1140
[8, 8, 8, 8, 8]
1130
1141
1131
- >>> import random
1132
1142
>>> take(5 , map (int , repeatfunc(random.random)))
1133
1143
[0, 0, 0, 0, 0]
1134
1144
@@ -1156,10 +1166,28 @@ which incur interpreter overhead.
1156
1166
>>> all (factored(x) == expanded(x) for x in range (- 10 , 11 ))
1157
1167
True
1158
1168
1169
+ >>> list (sieve(30 ))
1170
+ [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
1171
+ >>> small_primes = [2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 ]
1172
+ >>> all (list (sieve(n)) == [p for p in small_primes if p < n] for n in range (60 ))
1173
+ True
1174
+ >>> len (list (sieve(100 )))
1175
+ 25
1176
+ >>> len (list (sieve(1_000 )))
1177
+ 168
1178
+ >>> len (list (sieve(10_000 )))
1179
+ 1229
1180
+ >>> len (list (sieve(100_000 )))
1181
+ 9592
1182
+ >>> len (list (sieve(1_000_000 )))
1183
+ 78498
1184
+ >>> carmichael = {561 , 1105 , 1729 , 2465 , 2821 , 6601 , 8911 } # https://oeis.org/A002997
1185
+ >>> set (sieve(10_000 )).isdisjoint(carmichael)
1186
+ True
1187
+
1159
1188
>>> list (flatten([(' a' , ' b' ), (), (' c' , ' d' , ' e' ), (' f' ,), (' g' , ' h' , ' i' )]))
1160
1189
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
1161
1190
1162
- >>> import random
1163
1191
>>> random.seed(85753098575309 )
1164
1192
>>> list (repeatfunc(random.random, 3 ))
1165
1193
[0.16370491282496968, 0.45889608687313455, 0.3747076837820118]
0 commit comments