mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
421ace81ed
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.0.285 → v0.0.286](https://github.com/astral-sh/ruff-pre-commit/compare/v0.0.285...v0.0.286) - [github.com/tox-dev/pyproject-fmt: 0.13.1 → 1.1.0](https://github.com/tox-dev/pyproject-fmt/compare/0.13.1...1.1.0) * updating DIRECTORY.md * Fis ruff rules PIE808,PLR1714 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
"""
|
|
The stock span problem is a financial problem where we have a series of n daily
|
|
price quotes for a stock and we need to calculate span of stock's price for all n days.
|
|
|
|
The span Si of the stock's price on a given day i is defined as the maximum
|
|
number of consecutive days just before the given day, for which the price of the stock
|
|
on the current day is less than or equal to its price on the given day.
|
|
"""
|
|
|
|
|
|
def calculation_span(price, s):
|
|
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))
|