Skip to content
Merged
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bda3f0c
Adding doctests in simpson_rule.py
AasheeshLikePanner Oct 15, 2023
2186089
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
50ecc03
Update stock_span_problem.py
AasheeshLikePanner Oct 15, 2023
463d0cb
Merge remote-tracking branch 'origin/StockAndSpan_Doctests' into Stoc…
AasheeshLikePanner Oct 15, 2023
c0e2b16
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
76d97c9
Update subset_generation.py
AasheeshLikePanner Oct 15, 2023
3e5a3f4
Merge remote-tracking branch 'origin/StockAndSpan_Doctests' into Stoc…
AasheeshLikePanner Oct 15, 2023
136bf70
Update subset_generation.py
AasheeshLikePanner Oct 15, 2023
8ab4033
Update data_structures/stacks/stock_span_problem.py
AasheeshLikePanner Oct 15, 2023
ad99674
Update stock_span_problem.py
AasheeshLikePanner Oct 15, 2023
c76afc8
Merge remote-tracking branch 'origin/StockAndSpan_Doctests' into Stoc…
AasheeshLikePanner Oct 15, 2023
6bd76ea
Update data_structures/stacks/stock_span_problem.py
AasheeshLikePanner Oct 16, 2023
6a230a5
Update stock_span_problem.py
AasheeshLikePanner Oct 16, 2023
3ba3cf7
Update stock_span_problem.py
AasheeshLikePanner Oct 16, 2023
ec23923
Merge remote-tracking branch 'origin/StockAndSpan_Doctests' into Stoc…
AasheeshLikePanner Oct 16, 2023
caa4f56
Merge branch 'master' into StockAndSpan_Doctests
MaximSmolskiy Aug 29, 2025
b756caa
updating DIRECTORY.md
MaximSmolskiy Aug 29, 2025
01e2cd4
Update stock_span_problem.py
MaximSmolskiy Aug 29, 2025
8a5557c
Update stock_span_problem.py
MaximSmolskiy Aug 29, 2025
6dafa54
Update stock_span_problem.py
MaximSmolskiy Aug 29, 2025
c5064b4
Update stock_span_problem.py
MaximSmolskiy Aug 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 38 additions & 19 deletions data_structures/stacks/stock_span_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,62 @@


def calculation_span(price, s):
"""
Calculate the span values for a given list of stock prices.
Args:
price (list): List of stock prices.
s (list): List to store the span values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you forcing to caller to allocate this memory? Why not do this in the function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AasheeshLikePanner didn't write the code in that way. The code was already like that back when it was first contributed to the repo. The code had the caller allocate the memory because it was directly copied (and thus plagiarized) from the GeeksForGeeks article you linked a few hours ago.

Returns:
None
>>> price = [10, 4, 5, 90, 120, 80]
>>> S = [0 for i in range(len(price) + 1)]
>>> calculation_span(price, S)
>>> S
[1, 1, 2, 4, 5, 6, 0]
>>> price = [100, 50, 60, 70, 80, 90]
>>> S = [0 for i in range(len(price) + 1)]
>>> calculation_span(price, S)
>>> S
[1, 1, 2, 3, 4, 5, 0]
>>> price = [5, 4, 3, 2, 1]
>>> S = [0 for i in range(len(price) + 1)]
>>> calculation_span(price, S)
>>> S
[1, 1, 2, 3, 4, 0]
>>> price = [1, 2, 3, 4, 5]
>>> S = [0 for i in range(len(price) + 1)]
>>> calculation_span(price, S)
>>> S
[1, 2, 3, 4, 5, 0]
>>> price = [10, 20, 30, 40, 50]
>>> S = [0 for i in range(len(price) + 1)]
>>> calculation_span(price, S)
>>> S
[1, 2, 3, 4, 5, 0]
"""
n = len(price)
# Create a stack and push index of fist element to it
st = []
st.append(0)

# Span value of first element is always 1
s[0] = 1

# Calculate span values for rest of the elements
for i in range(1, n):
# Pop elements from stack while stack is not
# empty and top of stack is smaller than price[i]
while len(st) > 0 and price[st[0]] <= price[i]:
st.pop()

# If stack becomes empty, then price[i] is greater
# than all elements on left of it, i.e. price[0],
# price[1], ..price[i-1]. Else the price[i] is
# greater than elements after top of stack
s[i] = i + 1 if len(st) <= 0 else (i - st[0])

# Push this element to stack
st.append(i)


# A utility function to print elements of array
def print_array(arr, n):
for i in range(n):
print(arr[i], end=" ")


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

# Fill the span values in array S[]
calculation_span(price, S)

# Print the calculated span values
print_array(S, len(price))

if __name__ == "__main__":
import doctest

doctest.testmod()