@@ -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):
@@ -809,23 +809,13 @@ which incur interpreter overhead.
809
809
for k in range(len(roots) + 1)
810
810
]
811
811
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
-
822
812
def sieve(n):
823
813
"Primes less than n"
824
814
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
825
815
data = bytearray([1]) * n
826
816
data[:2] = 0, 0
827
817
limit = math.isqrt(n) + 1
828
- for p in compress(range(limit ), data):
818
+ for p in compress(count( ), islice( data, limit) ):
829
819
data[p+p : n : p] = bytearray(len(range(p+p, n, p)))
830
820
return compress(count(), data)
831
821
@@ -866,12 +856,12 @@ which incur interpreter overhead.
866
856
867
857
def triplewise(iterable):
868
858
"Return overlapping triplets from an iterable"
869
- # triplewise('ABCDEFG') -> ABC BCD CDE DEF EFG
859
+ # triplewise('ABCDEFG') -- > ABC BCD CDE DEF EFG
870
860
for (a, _), (b, c) in pairwise(pairwise(iterable)):
871
861
yield a, b, c
872
862
873
863
def sliding_window(iterable, n):
874
- # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
864
+ # sliding_window('ABCDEFG', 4) -- > ABCD BCDE CDEF DEFG
875
865
it = iter(iterable)
876
866
window = collections.deque(islice(it, n), maxlen=n)
877
867
if len(window) == n:
@@ -1103,6 +1093,7 @@ which incur interpreter overhead.
1103
1093
>>> import operator
1104
1094
>>> import collections
1105
1095
>>> import math
1096
+ >>> import random
1106
1097
1107
1098
>>> take(10 , count())
1108
1099
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -1152,7 +1143,6 @@ which incur interpreter overhead.
1152
1143
>>> list (repeatfunc(pow , 5 , 2 , 3 ))
1153
1144
[8, 8, 8, 8, 8]
1154
1145
1155
- >>> import random
1156
1146
>>> take(5 , map (int , repeatfunc(random.random)))
1157
1147
[0, 0, 0, 0, 0]
1158
1148
@@ -1180,20 +1170,8 @@ which incur interpreter overhead.
1180
1170
>>> all (factored(x) == expanded(x) for x in range (- 10 , 11 ))
1181
1171
True
1182
1172
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
-
1192
1173
>>> list (sieve(30 ))
1193
1174
[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
1197
1175
>>> len (list (sieve(100 )))
1198
1176
25
1199
1177
>>> len (list (sieve(1_000 )))
@@ -1204,14 +1182,10 @@ which incur interpreter overhead.
1204
1182
9592
1205
1183
>>> len (list (sieve(1_000_000 )))
1206
1184
78498
1207
- >>> carmichael = {561 , 1105 , 1729 , 2465 , 2821 , 6601 , 8911 } # https://oeis.org/A002997
1208
- >>> set (sieve(10_000 )).isdisjoint(carmichael)
1209
- True
1210
1185
1211
1186
>>> list (flatten([(' a' , ' b' ), (), (' c' , ' d' , ' e' ), (' f' ,), (' g' , ' h' , ' i' )]))
1212
1187
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
1213
1188
1214
- >>> import random
1215
1189
>>> random.seed(85753098575309 )
1216
1190
>>> list (repeatfunc(random.random, 3 ))
1217
1191
[0.16370491282496968, 0.45889608687313455, 0.3747076837820118]
0 commit comments