Question
SOLVE USING PYTHON Weekly Prices prices is a list of integers that represents the weekly prices. The goal is to compute a new list weeks
SOLVE USING PYTHON
Weekly Prices
prices is a list of integers that represents the weekly prices. The goal is to compute a new list weeks of the same length as prices, where weeks[i] is the number of weeks you need to wait after the i-th week in order to obtain a higher prices. weeks[i] should be 0 if there is no such future week. The provided template Prices.pyDownload Prices.py can read the input file and convert the input string into a list. You need to implement a function that takes a list as input and outputs a list. The template can help you to print the output list.
Example 1:
Input:
73 74 75 71 69 72 76 73
Output:
1 1 4 2 1 1 0 0
Explanation: In the first week, prices[0] = 73, you need to wait for 1 week to get a higher price (74), so weeks[0] = 1. In the third week, prices[2] = 75, you need to wait for 4 weeks to get a higher price (76), so weeks[2] = 4.
Example 2:
Input:
30 40 50 60
Output:
1 1 1 0
Template Code:
import sys# Input: prices is a list of integers# Output: weeks is a list of integers representing the number of weeks you need to wait fordef compute_weeks(prices): # TODO: Implement me! return weeksdef main(): # read the input file from stdin line = sys.stdin.readline().strip() # convert string to a list of integers prices = [int(v) for v in line.split()] weeks = compute_weeks(prices) weeks_str = ' '.join([str(v) for v in weeks]) print(weeks_str)# This line above main is for grading purposes. It will not affect how# your code will run while you develop and test it.# DO NOT REMOVE THE LINE ABOVE MAINif __name__ == "__main__": main()
Step by Step Solution
3.37 Rating (156 Votes )
There are 3 Steps involved in it
Step: 1
To solve this problem you can iterate through the list of prices and for each week find the first we...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