|
8 | 8 | """ |
9 | 9 |
|
10 | 10 |
|
11 | | -def calculation_span(price: list[float]): |
| 11 | +def calculation_span(price: list[float]) -> list[float]: |
12 | 12 | """ |
13 | 13 | Calculate the span values for a given list of stock prices. |
14 | 14 | Args: |
15 | 15 | price (list): List of stock prices. |
16 | 16 | Returns: |
17 | 17 | >>> price = [10, 4, 5, 90, 120, 80] |
18 | 18 | >>> calculation_span(price) |
19 | | - [1, 1, 2, 4, 5, 6] |
| 19 | + [1.0, 1.0, 2.0, 4.0, 5.0, 6.0] |
20 | 20 | >>> price = [100, 50, 60, 70, 80, 90] |
21 | 21 | >>> calculation_span(price) |
22 | | - [1, 1, 2, 3, 4, 5] |
| 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, 1, 2, 3, 4] |
| 25 | + [1.0, 1.0, 2.0, 3.0, 4.0] |
26 | 26 | >>> price = [1, 2, 3, 4, 5] |
27 | 27 | >>> calculation_span(price) |
28 | | - [1, 2, 3, 4, 5] |
| 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 | | - [1, 2, 3, 4, 5] |
| 31 | + [1.0, 2.0, 3.0, 4.0, 5.0] |
32 | 32 | """ |
33 | 33 | n = len(price) |
34 | 34 | st = [0] |
35 | | - s = [0] * n |
36 | | - s[0] = 1 |
| 35 | + s = [0.0] * n |
| 36 | + s[0] = 1.0 |
37 | 37 | for i in range(1, n): |
38 | 38 | while len(st) > 0 and price[st[0]] <= price[i]: |
39 | 39 | st.pop() |
40 | | - s[i] = i + 1 if len(st) <= 0 else (i - st[0]) |
| 40 | + s[i] = float(i + 1) if len(st) <= 0 else float(i - st[0]) |
41 | 41 | return s |
42 | 42 |
|
43 | 43 |
|
|
0 commit comments