Skip to content

Commit 278030a

Browse files
authored
Misc improvements to the itertools docs (GH-104916)
1 parent 8557a97 commit 278030a

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

Diff for: Doc/library/itertools.rst

+24-7
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ loops that truncate the stream.
147147
>>> list(accumulate(data, max)) # running maximum
148148
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]
149149

150-
# Amortize a 5% loan of 1000 with 4 annual payments of 90
151-
>>> cashflows = [1000, -90, -90, -90, -90]
152-
>>> list(accumulate(cashflows, lambda bal, pmt: bal*1.05 + pmt))
153-
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
150+
# Amortize a 5% loan of 1000 with 10 annual payments of 90
151+
>>> account_update = lambda bal, pmt: round(bal * 1.05) + pmt
152+
>>> list(accumulate(repeat(-90, 10), account_update, initial=1_000))
153+
[1000, 960, 918, 874, 828, 779, 728, 674, 618, 559, 497]
154154

155155
See :func:`functools.reduce` for a similar function that returns only the
156156
final accumulated value.
@@ -951,7 +951,10 @@ which incur interpreter overhead.
951951
nexts = cycle(islice(nexts, num_active))
952952
953953
def partition(pred, iterable):
954-
"Use a predicate to partition entries into false entries and true entries"
954+
"""Partition entries into false entries and true entries.
955+
956+
If *pred* is slow, consider wrapping it with functools.lru_cache().
957+
"""
955958
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
956959
t1, t2 = tee(iterable)
957960
return filterfalse(pred, t1), filter(pred, t2)
@@ -1031,7 +1034,7 @@ The following recipes have a more mathematical flavor:
10311034
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
10321035

10331036
def sieve(n):
1034-
"Primes less than n"
1037+
"Primes less than n."
10351038
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
10361039
data = bytearray((0, 1)) * (n // 2)
10371040
data[:3] = 0, 0, 0
@@ -1068,7 +1071,7 @@ The following recipes have a more mathematical flavor:
10681071
10691072
def matmul(m1, m2):
10701073
"Multiply two matrices."
1071-
# matmul([(7, 5), (3, 5)], [[2, 5], [7, 9]]) --> (49, 80), (41, 60)
1074+
# matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]) --> (49, 80), (41, 60)
10721075
n = len(m2[0])
10731076
return batched(starmap(math.sumprod, product(m1, transpose(m2))), n)
10741077

@@ -1109,6 +1112,17 @@ The following recipes have a more mathematical flavor:
11091112
powers = map(pow, repeat(x), reversed(range(n)))
11101113
return math.sumprod(coefficients, powers)
11111114

1115+
def polynomial_derivative(coefficients):
1116+
"""Compute the first derivative of a polynomial.
1117+
1118+
f(x) = x³ -4x² -17x + 60
1119+
f'(x) = 3x² -8x -17
1120+
"""
1121+
# polynomial_derivative([1, -4, -17, 60]) -> [3, -8, -17]
1122+
n = len(coefficients)
1123+
powers = reversed(range(1, n))
1124+
return list(map(operator.mul, coefficients, powers))
1125+
11121126
def nth_combination(iterable, r, index):
11131127
"Equivalent to list(combinations(iterable, r))[index]"
11141128
pool = tuple(iterable)
@@ -1297,6 +1311,9 @@ The following recipes have a more mathematical flavor:
12971311
>>> all(factored(x) == expanded(x) for x in range(-10, 11))
12981312
True
12991313

1314+
>>> polynomial_derivative([1, -4, -17, 60])
1315+
[3, -8, -17]
1316+
13001317
>>> list(iter_index('AABCADEAF', 'A'))
13011318
[0, 1, 4, 7]
13021319
>>> list(iter_index('AABCADEAF', 'B'))

0 commit comments

Comments
 (0)