Skip to content

Commit 8883596

Browse files
authored
Add built-in approach for Prefix sums
1 parent 980b495 commit 8883596

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

content/3_Silver/Prefix_Sums.mdx

+5-1
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,16 @@ public class Main {
272272
273273
<PySection>
274274
275+
In Python you can use [itertools.accumulate](https://docs.python.org/3.6/library/itertools.html#itertools.accumulate). Note that you have to call list() since accumulate returns a [generator](https://wiki.python.org/moin/Generators). The first value returned is that of the iterable passed, so we add a zero in the front (in newer versions you can give the optional parameter of initial=0).
276+
275277
```py
278+
# import itertools
279+
276280
def psum(a):
277281
psum = [0]
278282
for i in a:
279283
psum.append(psum[-1] + i) # psum[-1] is the last element in the list
280-
return psum
284+
return psum # Or with itertools.accumulate: [0] + list(itertools.accumulate(a))
281285

282286

283287
N, Q = map(int, input().split())

0 commit comments

Comments
 (0)