@@ -16,28 +16,30 @@ def calculation_span(price: list[float]) -> list[float]:
16
16
Returns:
17
17
>>> price = [10, 4, 5, 90, 120, 80]
18
18
>>> calculation_span(price)
19
- [1.0, 1.0, 2.0, 4.0, 5.0, 6 .0]
19
+ [1.0, 1.0, 2.0, 4.0, 5.0, 1 .0]
20
20
>>> price = [100, 50, 60, 70, 80, 90]
21
21
>>> calculation_span(price)
22
22
[1.0, 1.0, 2.0, 3.0, 4.0, 5.0]
23
23
>>> price = [5, 4, 3, 2, 1]
24
24
>>> calculation_span(price)
25
- [1.0, 1.0, 2 .0, 3 .0, 4 .0]
25
+ [1.0, 1.0, 1 .0, 1 .0, 1 .0]
26
26
>>> price = [1, 2, 3, 4, 5]
27
27
>>> calculation_span(price)
28
28
[1.0, 2.0, 3.0, 4.0, 5.0]
29
29
>>> price = [10, 20, 30, 40, 50]
30
30
>>> calculation_span(price)
31
31
[1.0, 2.0, 3.0, 4.0, 5.0]
32
+ >>> calculation_span(price=[100, 80, 60, 70, 60, 75, 85])
33
+ [1.0, 1.0, 1.0, 2.0, 1.0, 4.0, 6.0]
32
34
"""
33
35
n = len (price )
34
36
st = [0 ]
35
- s = [0.0 ] * n
36
- s [0 ] = 1.0
37
+ s = [1.0 ]
37
38
for i in range (1 , n ):
38
- while len ( st ) > 0 and price [st [0 ]] <= price [i ]:
39
+ while st and price [st [- 1 ]] <= price [i ]:
39
40
st .pop ()
40
- s [i ] = float (i + 1 ) if len (st ) <= 0 else float (i - st [0 ])
41
+ s .append (float (i - st [- 1 ] if st else i + 1 ))
42
+ st .append (i )
41
43
return s
42
44
43
45
0 commit comments