max array

🏠

Iterative

1A = [0,1,0,2,1,0,1,3,2,1,2,1]
2max_l, m = [], 0
3for a in A:
4    m = max(a, m)
5    max_l.append(m)
6print(max_l)

Output:

1[0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]

Generative

1A = [0,1,0,2,1,0,1,3,2,1,2,1]
2def max_array_from_left(A):
3    m = 0
4    for a in A:
5        m = max(a, m)
6        yield m
7
8print([*max_array_from_left(A)])

Output:

1[0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]

Functional

1from itertools import accumulate
2
3A = [0,1,0,2,1,0,1,3,2,1,2,1]
4
5accumulate(A, lambda a, b: b if b > a else a)

Output:

1[0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]

See also

max array from right