Stock Market
For this program you are given a String that represents stock prices for a particular com- y over a period of time. For example, you may be given a String that looks like the following:
String stockPrices = "1,22,30,38,44,68,49,73,52,66";
Each number in this String represents the stock price for a particular day. The first number is day 0, the second number is day 1, and so on. You can al supplied string will formatted using commas to separate daily stock prices. The individual stock prices will always be valid integers, but they many not always be the same number of digits (i.e. 1 is a valid stock price, as is 1000. Your program should work with strings of any length. The "split" method might come in handy for this problem. Your task is to analyze the supplied string and determine the following:
• The highest price for the stock
• The day when the highest price occurred
• The lowest price for the stock
• The day when the lowest price occurred
Here are two sample runnings of the program:
1 // First run
2 Please enter stock prices: 1,22,30,38,44,68,49,73,52,66
3 Highest price: 73 occurred on day # 7
4 Lowest price: 1 occurred on day # 0
5 // second run
6 Please enter stock prices: stock_prices = 41,37,40,54,51,63,54,47,23,33
7 Highest price: 63 occurred on day # 5
8 Lowest price: 23 occurred on day # 8

Respuesta :

Answer:

stockprice = input("Please enter stock prices: ")

prices = stockprice.split(",")

least = int(prices[0])

highest = int(prices[0])

for price in prices:

    if int(price) < least:

         least=int(price)

    if int(price) > highest:

         highest=int(price)

index = prices.index(str(highest))

print("Highest price: "+str(highest)+" ocurred on day # "+str(index))

index = prices.index(str(least))

print("Lowest price: "+str(least)+" ocurred on day # "+str(index))

Explanation:

I added the explanation as an attachment where comments were used to explain each line one after the other.

PS: The question is answered in Python

Ver imagen MrRoyal
ACCESS MORE
EDU ACCESS