From ccb25641ba5ebebbe27a4155f1aff381f5f035d0 Mon Sep 17 00:00:00 2001 From: S-Sanyal Date: Tue, 2 Oct 2018 14:57:37 +0530 Subject: [PATCH 1/2] Added Stock-Span-Problem --- data_structures/Stacks/Stock-Span-Problem.py | 51 ++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 data_structures/Stacks/Stock-Span-Problem.py diff --git a/data_structures/Stacks/Stock-Span-Problem.py b/data_structures/Stacks/Stock-Span-Problem.py new file mode 100644 index 000000000..658ac3cbf --- /dev/null +++ b/data_structures/Stacks/Stock-Span-Problem.py @@ -0,0 +1,51 @@ +''' +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 calculateSpan(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 whlie 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 printArray(arr, n): + for i in range(0,n): + print arr[i], + + +# 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[] +calculateSpan(price, S) + +# Print the calculated span values +printArray(S, len(price)) \ No newline at end of file From 8fb4df5367b5c03d2851532063f6fa781fe2f980 Mon Sep 17 00:00:00 2001 From: Purvesh Makode Date: Tue, 2 Oct 2018 18:14:53 +0530 Subject: [PATCH 2/2] Add Fibonacci Series Using Recursion --- Maths/fibonacciSeries.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Maths/fibonacciSeries.py diff --git a/Maths/fibonacciSeries.py b/Maths/fibonacciSeries.py new file mode 100644 index 000000000..cf025b07d --- /dev/null +++ b/Maths/fibonacciSeries.py @@ -0,0 +1,16 @@ +# Fibonacci Sequence Using Recursion + +def recur_fibo(n): + if n <= 1: + return n + else: + return(recur_fibo(n-1) + recur_fibo(n-2)) + +limit = int(input("How many terms to include in fionacci series:")) + +if limit <= 0: + print("Plese enter a positive integer") +else: + print("Fibonacci series:") + for i in range(limit): + print(recur_fibo(i))