Skip to content

Commit 50ecc03

Browse files
Update stock_span_problem.py
1 parent bda3f0c commit 50ecc03

1 file changed

Lines changed: 16 additions & 35 deletions

File tree

data_structures/stacks/stock_span_problem.py

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,42 @@
88
"""
99

1010

11-
def calculation_span(price, s):
11+
def calculation_span(price):
1212
"""
1313
Calculate the span values for a given list of stock prices.
1414
Args:
1515
price (list): List of stock prices.
16-
s (list): List to store the span values.
1716
Returns:
18-
None
1917
>>> price = [10, 4, 5, 90, 120, 80]
20-
>>> S = [0 for i in range(len(price) + 1)]
21-
>>> calculation_span(price, S)
22-
>>> S
23-
[1, 1, 2, 4, 5, 6, 0]
18+
>>> calculation_span(price)
19+
[1, 1, 2, 4, 5, 6]
2420
>>> price = [100, 50, 60, 70, 80, 90]
25-
>>> S = [0 for i in range(len(price) + 1)]
26-
>>> calculation_span(price, S)
27-
>>> S
28-
[1, 1, 2, 3, 4, 5, 0]
21+
>>> calculation_span(price)
22+
[1, 1, 2, 3, 4, 5]
2923
>>> price = [5, 4, 3, 2, 1]
30-
>>> S = [0 for i in range(len(price) + 1)]
31-
>>> calculation_span(price, S)
32-
>>> S
33-
[1, 1, 2, 3, 4, 0]
24+
>>> calculation_span(price)
25+
[1, 1, 2, 3, 4]
3426
>>> price = [1, 2, 3, 4, 5]
35-
>>> S = [0 for i in range(len(price) + 1)]
36-
>>> calculation_span(price, S)
37-
>>> S
38-
[1, 2, 3, 4, 5, 0]
27+
>>> calculation_span(price)
28+
[1, 2, 3, 4, 5]
3929
>>> price = [10, 20, 30, 40, 50]
40-
>>> S = [0 for i in range(len(price) + 1)]
41-
>>> calculation_span(price, S)
42-
>>> S
43-
[1, 2, 3, 4, 5, 0]
30+
>>> calculation_span(price)
31+
[1, 2, 3, 4, 5]
4432
"""
4533
n = len(price)
46-
st = []
47-
st.append(0)
34+
st = [0]
35+
s = [0] * n
4836
s[0] = 1
4937
for i in range(1, n):
5038
while len(st) > 0 and price[st[0]] <= price[i]:
5139
st.pop()
5240
s[i] = i + 1 if len(st) <= 0 else (i - st[0])
53-
54-
55-
def print_array(arr, n):
56-
for i in range(n):
57-
print(arr[i], end=" ")
41+
return s
5842

5943

6044
price = [10, 4, 5, 90, 120, 80]
61-
S = [0 for i in range(len(price) + 1)]
62-
63-
calculation_span(price, S)
64-
65-
print_array(S, len(price))
45+
S = calculation_span(price)
46+
print(S)
6647

6748
if __name__ == "__main__":
6849
import doctest

0 commit comments

Comments
 (0)