Question

In: Computer Science

Please write simple python program with explanation Problem statement: Stock markets are venues where buyers and...

Please write simple python program with explanation

Problem statement:

Stock markets are venues where buyers and sellers of shares meet and decide on a price to trade.Eddard is very interested in trading in stock market and he started gathering information of one stock.He has projected market prices for that share over the next n months.Eddard wants to purchase and resell the share to earn profits.He is much worried about the loss that might occur so he wanted to calculate "Minimum loss" according to following rules :

1.The share cannot be sold at a price greater than or equal to the price it was purchased at(i.e. it must be resold at a loss).

2.The share cannot be resold within the same month it was purchased.

Find and print the minimum amount of money Eddard must lose if he buys the share and resells it within the next n months.

Input Format:

The first line contains an integer n,denoting the number of months of share data.

The second line contains n space separated long integers describing the respective values of p1,p2,...pn.

Constraints:

1<=n<=10^5

1<=pi<=10^16

All the market prices are distinct and valid answer exists.

Output Format:

Print a single integer denoting the minimum amount of money Eddard must lose if he buys the share and resells it within the next n months.

Sample Input1:

3

5 10 3

Sample Output1:

2

Explanation:

Eddard buys the stock in month1 at price p1=5 and sells it in month 3 at p3 for a minimal loss of 5-3 = 2 .

Sample Output2:

5

77 82 76 88 80

Sample Output2:

1

Solutions

Expert Solution

n = int(input())                          #taking input of number of month
stocks= list(map(int, input().split()))   #storing the stock price of each month in list
diff = []                                 #declaring list to store the difference
for i in range(len(stocks)-1):            #Outer for loop for initial stocks
  for j in range(len(stocks)-1,i,-1):     #Inner for loop for later stocks
    if(stocks[i]>stocks[j]):              #Condition for the price of stock of next month to be less than the current month
      diff.append(stocks[i]-stocks[j])    #Calculating the difference between current month's and later month's stock price and appending it to list diff
if(len(diff)==0):                         #Checking for situation in which stock price didn't decrease in the next month
  print("Stock can't be sold as there's no month in which stock's price is decreasing") 
else:
  print(min(diff))                       #Printing the minimum value of the list diff in order to determine the minimum loss

Screen shot of code and output:

Explanation:

  1. As mentioned the stock is to be sold with loss only, hence we are required to calculate the minimum loss.
  2. Also the stock purchased in a month can be sold in next month only.
  3. In order to satisfy the condition mentioned in the above point two for loop are used.
  4. The outer for loop iterates through the initial stock's price excluding the stock of the last month as there is no month next to the last month and stocks are not allowed to be sold in the same month.
  5. Whereas the inner for loop iterates through stock price of next month excluding the current and the previous month as difference will be calculated only between the current and next stock price as stocks are allowed to be sold only in next month.
  6. In order to sell it in loss the price of stock in next month should be less than the current month, this condition is checked in the inner for loop.
  7. The difference is calculated and appended to the list diff only if the price of next month is less than the current month.
  8. After the for loops, both inner and outer complete their iterations the conditions are checked if there was no decrease in stock price, as the stocks can only be sold in loss and in next month and difference calculated only if the price of the stock of the next month is less than the current month.
  9. In case there wasn't any decrease in price, there won't be any element in list diff(len(diff)==0), the print function will print "Stock can't be sold as there's no month in which stock's price is decreasing"
  10. Otherwise the minimum difference that is the minimum loss will be printed by finding the minimum element of list diff using function min() and passing the whole thing as a parameter to the function print().

Related Solutions

PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and prints/displays the computed values. The program will behave as in the following example. Note that in the two lines, Enter length and Enter width, the program does not display 10.0 or 8.0. They are values typed in by the user and read in by the program. The first two lines are text displayed by the program informing the user what the program does. This...
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
Program in Python Problem Statement Write a program with the following functions:  wordCount. This function...
Program in Python Problem Statement Write a program with the following functions:  wordCount. This function should accept a string as a parameter and return the number of words contained in the string.  mostFrequentWord. This function accepts a string as a parameter and returns the word that occurs the most frequently in the string.  replaceWord. This function accepts three strings as parameters, let’s call them string1, string2, and string3. It searches string1 for all occurrences of string2. When...
PLEASE INCLUDE #FOR EACH LINE EXPLANATION Write a PYTHON PROGRAM that prompts for an integer and...
PLEASE INCLUDE #FOR EACH LINE EXPLANATION Write a PYTHON PROGRAM that prompts for an integer and prints the integer, but if something other than an integer is input, the program keeps asking for an integer. Here is a sample session: Input an integer: abc Error: try again. Input an integer: 4a Error: try again. Input an integer: 2.5 Error: try again. Input an integer: 123 The integer is: 123 Hint: the string isdigit method will be useful to solve this...
please write simple python code Write a program to replicate the behavior of UNIX utility “tail”....
please write simple python code Write a program to replicate the behavior of UNIX utility “tail”. It takes one or more files and displays requested number of lines from the ending. If number is not specified, then it print 10 lines by default.
Python Problem Problem 1: In this problem you are asked to write a Python program to...
Python Problem Problem 1: In this problem you are asked to write a Python program to find the greatest and smallest elements in the list. The user gives the size of the list and its elements (positive and negative integers) as the input. Sample Output: Enter size of the list: 7 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 6 Enter element: 8 Enter element: 10 Enter element: 12 Greatest element in the list is: 12 Smallest...
please write simple python 3 program with explanations and correct output Bilbo and problems on the...
please write simple python 3 program with explanations and correct output Bilbo and problems on the board Problem Statement Bilbo once reached his maths claws earlier than anyone else.He saw some N unique numbers greater than 0 written on the board.He thought of challenging his classmates when they come.So for each number i,he wrote the number before and after it on a paper slip.If it is the last number then the number after it is assumed to be 0.If it...
Question(on Python). There is a Python program what could solve the simple slide puzzles problem by...
Question(on Python). There is a Python program what could solve the simple slide puzzles problem by A* algorithm. Please fill in the body of the a_star() function. In order to solve a specific problem, the a_star function needs to know the problem's start state, the desired goal state, and a function that expands a given node. To be precise, this function receives as its input the current state and the goal state and returns a list of pairs of the...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
Python English algorithm explanation Write a program that asks the user for the name of a...
Python English algorithm explanation Write a program that asks the user for the name of a file in the current directory. Then, open the file and process the content of the file. 1)If the file contains words that appear more than once, print “We found duplicates.” 2)If the file does not contain duplicate words, print “There are no duplicates.”
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT