Starting from:

$30

PROGRAMMING ASSIGNMENT 1 Solution




Programming Assignment



The span si of a stock’s price on a certain day i is the maximum number of consecutive days (up to and including the current day) that the price of the stock has been less than or equal to its price of that day i. Given an array P of stock prices such that P [i] is the price of a stock on day i, your job is to calculate the span for each day. Let’s call it the StockSpan problem. The input and output of the problem can be described formally as below.




Input: An array P of n non-negative integers, where P [i] is the price of a stock on day i.




Output: An array S of n positive numbers, where S[i] is the span of the stock price on day i.




A Java template has been provided containing an empty function CalculateSpan, which takes an integer array P as its only argument, and returns an integer array. Your task is to write the body of the CalculateSpan function. You may assume that the input array P will always conform to the speci cation above (containing no negative values). Your code is not required to check for incorrectly formed input data.




You must use the provided Java template as the basis of your submission, and put your im-plementation inside the CalculateSpan function in the template. You may not change the name, return type or parameters of the CalculateSpan function. The main function in the template contains code to help you test your implementation by entering test data or reading it from a le. You may modify the main function, but only the contents of the CalculateSpan function will be marked, since the main function will be deleted before marking begins. You can write new helper functions/methods and can also declare new classes if you need. But you can submit ONLY the le named \Stock.java". If you submit multiple les or change the name of the class or the CalculateSpan function (even lowercase to uppercase or vice versa), your submission may generate compilation error during the automated testing and you will be graded accordingly.




Examples



The table below shows the correct output of the CalculateSpan function on various test inputs.
















1






Input Array P
Output Array S




1;5;6;8;7;9
1;2;3;4;1;6
25; 26; 75; 14; 25; 65; 32; 85; 25; 65; 9; 45
1;2;3;1;2;3;1;8;1;2;1;2
78; 52; 21; 14; 1; 2; 9; 0
1;1;1;1;1;2;3;1







Evaluation Criteria



The programming assignment will be marked out of 50, based on a combination of automated testing and human inspection. You may safely assume that the maximum size of an input array will be 100000.




For an input array containing n values, a brute force solution to the StockSpan problem will have running time O(n2). But the optimal algorithm will have running time O(n). Hint: Use a stack to achieve the optimal running time.




The mark for each submission will be based on both the asymptotic worst case running time and the ability of the algorithm to handle inputs of di erent sizes. The table below shows the expectations associated with di erent scores.




Score
Description




0-10
Submission does not compile or does not conform to the provided


template.




11-25
The implemented algorithm is O(n2) or is substantially inaccurate


on the tested inputs.




26-50
The implemented algorithm is O(n). The numbers would vary


based on how many test cases your algorithm gives correct answer


to.







To be properly tested, every submission must compile correctly as submitted, and must be based on the provided template. If your submission does not compile for any reason (even trivial mistakes like typos), or was not based on the template, it will receive at most 10 out of 50. The best way to make sure your submission is correct is to download it from conneX after submitting and test it.




You are not permitted to revise your submission after the due date, and late submissions will not be accepted, so you should ensure that you have submitted the correct version of your code before the due date. conneX will allow you to change your submission before the due date if you notice a mistake. After submitting your assignment, conneX will automatically send you a con rmation email. If you do not receive such an email, your submission was not received. If you have problems with the submission process, send an email to the instructor before the due date.











































2

More products