TutorMe homepage
Subjects
PRICING
COURSES
SIGN IN
Start Free Trial
Shan M.
Data Scientist at Media iQ Digital
Tutor Satisfaction Guarantee
Python Programming
TutorMe
Question:

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.

Shan M.
Answer:

def calculateSpan(price, n, S): # Span value of first day is always 1 S[0] = 1 # Calculate span value of remaining days by linearly # checking previous days for i in range(1, n, 1): S[i] = 1 # Initialize span value # Traverse left while the next element on left is # smaller than price[i] j = i - 1 while (j>=0) and (price[i] >= price[j]) : S[i] += 1 j -= 1 def printArray(arr, n): for i in range(n): print(arr[i], end = " ") # Driver program to test above function price = [10, 4, 5, 90, 120, 80] n = len(price) S = [None] * n # Fill the span values in list S[] calculateSpan(price, n, S) # print the calculated span values printArray(S, n)

Java Programming
TutorMe
Question:

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.

Shan M.
Answer:

import java.util.Arrays; class GFG { // method to calculate stock span values static void calculateSpan(int price[], int n, int S[]) { // Span value of first day is always 1 S[0] = 1; // Calculate span value of remaining days by linearly checking // previous days for (int i = 1; i < n; i++) { S[i] = 1; // Initialize span value // Traverse left while the next element on left is smaller // than price[i] for (int j = i-1; (j>=0)&&(price[i]>=price[j]); j--) S[i]++; } } { System.out.print(Arrays.toString(arr)); } // Driver program to test above functions public static void main(String[] args) { int price[] = {10, 4, 5, 90, 120, 80}; int n = price.length; int S[]= new int[n]; // Fill the span values in array S[] calculateSpan(price, n, S); // print the calculated span values printArray(S); } }

C Programming
TutorMe
Question:

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.

Shan M.
Answer:

void calculateSpan(int price[], int n, int S[]) { // Span value of first day is always 1 S[0] = 1; // Calculate span value of remaining days by linearly checking // previous days for (int i = 1; i < n; i++) { S[i] = 1; // Initialize span value // Traverse left while the next element on left is smaller // than price[i] for (int j = i-1; (j>=0)&&(price[i]>=price[j]); j--) S[i]++; } } void printArray(int arr[], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); } // Driver program to test above function int main() { int price[] = {10, 4, 5, 90, 120, 80}; int n = sizeof(price)/sizeof(price[0]); int S[n]; // Fill the span values in array S[] calculateSpan(price, n, S); // print the calculated span values printArray(S, n); return 0; }

Send a message explaining your
needs and Shan will reply soon.
Contact Shan
Ready now? Request a lesson.
Start Session
FAQs
What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Session" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.