Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5c50572

Browse files
AasheeshLikePannerpre-commit-ci[bot]cclaussMaximSmolskiy
authored
Fixing stock_span_problem.py (#10540)
* Adding doctests in simpson_rule.py* [pre-commit.ci] auto fixes from pre-commit.com hooksfor more information, seehttps://pre-commit.ci* Update stock_span_problem.py* [pre-commit.ci] auto fixes from pre-commit.com hooksfor more information, seehttps://pre-commit.ci* Update subset_generation.py* Update subset_generation.py* Update data_structures/stacks/stock_span_problem.pyCo-authored-by: Christian Clauss <cclauss@me.com>* Update stock_span_problem.py* Update data_structures/stacks/stock_span_problem.pyCo-authored-by: Christian Clauss <cclauss@me.com>* Update stock_span_problem.py* Update stock_span_problem.py* updating DIRECTORY.md* Update stock_span_problem.py* Update stock_span_problem.py* Update stock_span_problem.py* Update stock_span_problem.py---------Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>Co-authored-by: Christian Clauss <cclauss@me.com>Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>Co-authored-by: MaximSmolskiy <MaximSmolskiy@users.noreply.github.com>
1 parent4394fd9 commit5c50572

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@
725725
*[Secant Method](maths/numerical_analysis/secant_method.py)
726726
*[Simpson Rule](maths/numerical_analysis/simpson_rule.py)
727727
*[Square Root](maths/numerical_analysis/square_root.py)
728+
*[Weierstrass Method](maths/numerical_analysis/weierstrass_method.py)
728729
*[Odd Sieve](maths/odd_sieve.py)
729730
*[Perfect Cube](maths/perfect_cube.py)
730731
*[Perfect Number](maths/perfect_number.py)

‎data_structures/stacks/stock_span_problem.py‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,29 @@
88
"""
99

1010

11-
defcalculation_span(price,s):
11+
defcalculate_span(price:list[int])->list[int]:
12+
"""
13+
Calculate the span values for a given list of stock prices.
14+
Args:
15+
price: List of stock prices.
16+
Returns:
17+
List of span values.
18+
19+
>>> calculate_span([10, 4, 5, 90, 120, 80])
20+
[1, 1, 2, 4, 5, 1]
21+
>>> calculate_span([100, 50, 60, 70, 80, 90])
22+
[1, 1, 2, 3, 4, 5]
23+
>>> calculate_span([5, 4, 3, 2, 1])
24+
[1, 1, 1, 1, 1]
25+
>>> calculate_span([1, 2, 3, 4, 5])
26+
[1, 2, 3, 4, 5]
27+
>>> calculate_span([10, 20, 30, 40, 50])
28+
[1, 2, 3, 4, 5]
29+
>>> calculate_span([100, 80, 60, 70, 60, 75, 85])
30+
[1, 1, 1, 2, 1, 4, 6]
31+
"""
1232
n=len(price)
33+
s= [0]*n
1334
# Create a stack and push index of fist element to it
1435
st= []
1536
st.append(0)
@@ -21,18 +42,20 @@ def calculation_span(price, s):
2142
foriinrange(1,n):
2243
# Pop elements from stack while stack is not
2344
# empty and top of stack is smaller than price[i]
24-
whilelen(st)>0andprice[st[0]]<=price[i]:
45+
whilelen(st)>0andprice[st[-1]]<=price[i]:
2546
st.pop()
2647

2748
# If stack becomes empty, then price[i] is greater
2849
# than all elements on left of it, i.e. price[0],
2950
# price[1], ..price[i-1]. Else the price[i] is
3051
# greater than elements after top of stack
31-
s[i]=i+1iflen(st)<=0else (i-st[0])
52+
s[i]=i+1iflen(st)<=0else (i-st[-1])
3253

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

57+
returns
58+
3659

3760
# A utility function to print elements of array
3861
defprint_array(arr,n):
@@ -42,10 +65,9 @@ def print_array(arr, n):
4265

4366
# Driver program to test above function
4467
price= [10,4,5,90,120,80]
45-
S= [0foriinrange(len(price)+1)]
4668

47-
#Fill the span values in array S[]
48-
calculation_span(price,S)
69+
#Calculate the span values
70+
S=calculate_span(price)
4971

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp