Question

In: Computer Science

Question: The function predictAnswer should be made based on the following. Your answer must be able...

Question:

The function predictAnswer should be made based on the following. Your answer must be able to run in LESS than quadratic time. Either NLogN or linear time is expected.

In the prediction game, the first player gives the second player some stock market data for some consecutive days. The data contains a company's stock price on each day. The rules for the game are:

  1. Player 1 will tell player 2 a day number
  2. Player 2 has to find the nearest day on which stock price is smaller than the given day
  3. If there are two results, then player 2 finds the day number which is smaller
  4. If no such day exits, then the answer is -1.

Example 1

stockData size n =10;

stockData = [5,6,8,4,9,10,8,3,6,4]

queries = [6,5,4]

Result is [5,4,8]

On day 6, the stock price is 10. Both 9 and 8 are lower prices one day away. Choose 9 (day 5) because it is before day 6. On day 5, the stock price is 9. 4 is the closest lower price on day 4. On day 4, the stock price is 4. The only lower price is on day 8. The return array is [5,4,8]

Example - 2

stockData size n = 10

stockData = [5,6,8,4,9,10,8,3,6,4]

queries = [3,1,8]

Result is [2,4,-1]

If the day number is 3.both days 2 and 4 are smaller.choose the earlier day,day 2.

If the day number is 1,day 4 is the closet day with a smaller price.

If the day number is 8,there is no day where the price is less than 3.

The return array is [2,4,-1]

Code Snippet:

/*

     * Complete the 'predictAnswer' function below.

     *

     * The function is expected to return an INTEGER_ARRAY.

     * The function accepts following parameters:

     *  1. INTEGER_ARRAY stockData

     *  2. INTEGER_ARRAY queries

     */

def predictAnswer(stockData, queries):

Solutions

Expert Solution

Python code for above problem

def predictAnswers(stockData,queries):
   result=[]
   for day in queries:
       left_index=day-2
       while(left_index>=0):
           if(stockData[left_index]<stockData[day-1]):
               break
           left_index-=1
      
       right_index=day
       while(right_index<len(stockData)):
           if(stockData[right_index]<stockData[day-1]):
               break
           right_index+=1
          
       min_val_1=99999999
       min_val_2=99999999
      
       if(left_index>=0):
           min_val_1=abs(left_index-day+1)
      
       if(right_index<len(stockData)):
           min_val_2=abs(right_index-day+1)
          
       min_val=min([min_val_1,min_val_2])
      
       if(min_val==99999999):
           result.append(-1)
       elif(min_val_1<=min_val_2):
           result.append(left_index+1)
       else:
           result.append(right_index+1)  
      
   return result
  
print(predictAnswers([5,6,8,4,9,10,8,3,6,4],[6,5,4]))   # prints [5,4,8]
print(predictAnswers([5,6,8,4,9,10,8,3,6,4],[3,1,8]))   # prints [2,4,-1]

Mention in comments if any mistakes or errors are found. Thank you.


Related Solutions

Question 4. Answer the following problem based on the Corporate Financial Analysis textbook. Your answer should...
Question 4. Answer the following problem based on the Corporate Financial Analysis textbook. Your answer should be formatted similarly to Figure 13-1 on page 403 of that textbook. (25 pts) Dinsmore Artists International is in the business of managing “pop” artists in the entertainment industry. It is considering the purchase of an executive jet plane to transport its executives and the artists it represents to various meetings and performance sites. It expects that by owning its own executive jet, it...
Question 5. Answer the following problem based on the Corporate Financial Analysis textbook. Your answer should...
Question 5. Answer the following problem based on the Corporate Financial Analysis textbook. Your answer should be formatted similarly to Figure 9-12 on page 307 of that textbook. (25 pts)       The Schroder Corporation must raise $3 million to finance the construction of a new facility. It plans to do this by increasing the number of shares of preferred and common stock and by issuing 10-year corporate bonds with a face value of $1000 and annual payments at a coupon...
Answer the following question in your own words. Your answer must have a 150 minimum word...
Answer the following question in your own words. Your answer must have a 150 minimum word count: Explain the difference between fixed costs and variable costs. Describe their significance and give examples of each one.
Answer the following question in your own words. Your answer must have a 150 minimum word...
Answer the following question in your own words. Your answer must have a 150 minimum word count: Define budgeting and describe its primary purposes and benefits to an organization.
Answer the following question in your own words. Your answer must have a 150 minimum word...
Answer the following question in your own words. Your answer must have a 150 minimum word count: Describe the three (3) primary methods of cost allocation and describe how they differ.
Answer the following question in your own words. Your answer must have a 150 minimum word...
Answer the following question in your own words. Your answer must have a 150 minimum word count: Describe the difference between financial accounting and managerial accounting. Give examples of the uses of each one.
Answer the following question in your own words. Your answer must have a 150 minimum word...
Answer the following question in your own words. Your answer must have a 150 minimum word count: Describe the differences between top-down and bottom-up budgeting.
1.Answer the following question based on a spiral of fatty acid catabolism: a.What is the function...
1.Answer the following question based on a spiral of fatty acid catabolism: a.What is the function of FAD in step 1 of the spiral? --------------------------------------------------- b.Identify the type of reaction instep 2 of the spiral? ------------------------------------------------------ c.What is the function of NAD+in step 3 of the spiral? --------------------------------------------------- d.What is the harvest of each spiral? -----------------------------------------------------------------------(note: last step is different) e.What is different about the harvest in the last step of the spiral? -------------------------------------- 2. Answer the following questions based...
You should be able to answer each of the following questions in a just a few...
You should be able to answer each of the following questions in a just a few sentences. Graphs or figures might be good to include if they help you to make a better argument or to explain your answer more clearly. 3. Grandma promises to sneak you some bourbon while your parents are cleaning up the Thanksgiving dishes, but only if you can impress her with something you learned in your economics class. Carefully explain to Grandma why the short...
(This is a law/legal related question, the answer must be based on IRAC model) QUESTION THREE...
(This is a law/legal related question, the answer must be based on IRAC model) QUESTION THREE “Judges don’t make law, they only apply it”. REQUIRED: Discuss this statement using case examples.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT