Skip to content

Commit bda3f0c

Browse files
Adding doctests in simpson_rule.py
1 parent ba828fe commit bda3f0c

1 file changed

Lines changed: 38 additions & 19 deletions

File tree

data_structures/stacks/stock_span_problem.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,62 @@
99

1010

1111
def calculation_span(price, s):
12+
"""
13+
Calculate the span values for a given list of stock prices.
14+
Args:
15+
price (list): List of stock prices.
16+
s (list): List to store the span values.
17+
Returns:
18+
None
19+
>>> 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]
24+
>>> 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]
29+
>>> 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]
34+
>>> 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]
39+
>>> 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]
44+
"""
1245
n = len(price)
13-
# Create a stack and push index of fist element to it
1446
st = []
1547
st.append(0)
16-
17-
# Span value of first element is always 1
1848
s[0] = 1
19-
20-
# Calculate span values for rest of the elements
2149
for i in range(1, n):
22-
# Pop elements from stack while stack is not
23-
# empty and top of stack is smaller than price[i]
2450
while len(st) > 0 and price[st[0]] <= price[i]:
2551
st.pop()
26-
27-
# If stack becomes empty, then price[i] is greater
28-
# than all elements on left of it, i.e. price[0],
29-
# price[1], ..price[i-1]. Else the price[i] is
30-
# greater than elements after top of stack
3152
s[i] = i + 1 if len(st) <= 0 else (i - st[0])
3253

33-
# Push this element to stack
34-
st.append(i)
3554

36-
37-
# A utility function to print elements of array
3855
def print_array(arr, n):
3956
for i in range(n):
4057
print(arr[i], end=" ")
4158

4259

43-
# Driver program to test above function
4460
price = [10, 4, 5, 90, 120, 80]
4561
S = [0 for i in range(len(price) + 1)]
4662

47-
# Fill the span values in array S[]
4863
calculation_span(price, S)
4964

50-
# Print the calculated span values
5165
print_array(S, len(price))
66+
67+
if __name__ == "__main__":
68+
import doctest
69+
70+
doctest.testmod()

0 commit comments

Comments
 (0)