data_structures.stacks.stock_span_problem

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.

Attributes

S

price

Functions

calculate_span(→ list[int])

Calculate the span values for a given list of stock prices.

print_array(arr, n)

Module Contents

data_structures.stacks.stock_span_problem.calculate_span(price: list[int]) list[int]

Calculate the span values for a given list of stock prices. Args:

price: List of stock prices.

Returns:

List of span values.

>>> calculate_span([10, 4, 5, 90, 120, 80])
[1, 1, 2, 4, 5, 1]
>>> calculate_span([100, 50, 60, 70, 80, 90])
[1, 1, 2, 3, 4, 5]
>>> calculate_span([5, 4, 3, 2, 1])
[1, 1, 1, 1, 1]
>>> calculate_span([1, 2, 3, 4, 5])
[1, 2, 3, 4, 5]
>>> calculate_span([10, 20, 30, 40, 50])
[1, 2, 3, 4, 5]
>>> calculate_span([100, 80, 60, 70, 60, 75, 85])
[1, 1, 1, 2, 1, 4, 6]
data_structures.stacks.stock_span_problem.print_array(arr, n)
data_structures.stacks.stock_span_problem.S = [0, 0, 0, 0, 0, 0]
data_structures.stacks.stock_span_problem.price = [10, 4, 5, 90, 120, 80]