Answered step by step
Verified Expert Solution
Question
1 Approved Answer
[10 marks] (The stock price problem) You have seen that the stack-based algorithm is much more efficient than the brute-force algorithm. In this question you
[10 marks] (The stock price problem) You have seen that the stack-based algorithm is much more efficient than the brute-force algorithm. In this question you are asked to implement a Python function that will return the numbers of days skipped for computing the up period. For example, for our favorite toy data: stockPrice = [7,12,9,6,3,9,10), the numbers of days skipped are [0,0,0,0,0,0,3]. For day 0 to 5, no days can be shipped. For day 6, however, the stack-based algorithm can skip day 2 to day 4, therefore skipping 3 days in the stack-based algorithm Note that this question appears to be similar to the last question in the examination last year. However, this question asks for a different result. In your implementation, you have to use stack py available under the Content of Blackboard, and you cannot change it. Furthermore, you can push only the days (i.e., integers starting from 0) into the stack. def daySaving (stock): A function to return the number of days skipped by the stack-based algorithm when computing the up period for each day Input: A list of stock prices Output: A list of the number of days skipped by the stack-based algorithm Include the code below for testing your function: stock = [7, 12, 9, 6, 3, 9, 10] print (daySaving (stock)) stock = [12,10,9,9,7,6,3] print (daySaving (stock) ) stock = 13,6,7,9,9,10,12] print (daySaving (stock)) stock = [7, 12, 9, 6, 3, 9, 10, 11, 13] print (daySaving (stock)) The expected outputs should be: [0, 0, 0, 0, 0, 0, 3] [0, 0, 0, 0, 0, 0, 0] [0, 0, 1, 2, 3, 4, 5] [0, 0, 0, 0, 0, 0, 3, 4, 6] Recall the function that implements the stack-based algorithm: import stack def stockUp (stock): up = len(stock) *[0] aStack = stack.Stack() up [0] = 1 aStack.push(0) for k in range (1, len (stock)): while not astack.isEmpty() and stock[aStack-peek())
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started