Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me to write these program in python, and it will pass all the test cases. Alfons wants to set up a hot dog

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Please help me to write these program in python, and it will pass all the test cases.

Alfons wants to set up a hot dog stand. He has done some research to find out how many Czech crowns (K) people are willing to pay for a hot dog. This has yielded a large set of integers, each of which indicates the maximum price that a certain person will pay. Alfons wants to set his price to maximize his revenue. If the price is too low, almost everyone will buy a hot dog, but the income from each hot dog he sells will be small. If the price is too high, few people will buy a hot dog and so Alfons's total revenue will also be small. The program's input consists of integers in the range from 1 to 5000 on one or more lines, separated by whitespace. Each of these indicates the maximum price that one customer has said he/she will pay for a hot dog. The numbers in the input are not sorted in any way, and values may appear more than once. The program should write a single integer to standard output: the selling price of a single hot dog such that Alfons's revenue will be as large as possible. If there is more than one price that will achieve the maximum revenue, the program should write the lowest of these. Sample input \#1: Explanation: At a price of 20Kc Alfons will sell 7 hot dogs and earn 140Kc. This is the maximum possible revenue. For example, if he raises his price to 23Kc, he will sell only 3 hot dogs and earn only 323Kcc=69Kc. If he lowers his price to 15Kc, he will sell 9 hot dogs, but his revenue will be only 915Kc=135 K. Sample input \#2: Output: Explanation: At a price of 15 K Alfons will sell 2 hot dogs and earn 30 K. At a price of 16 K he will sell 1 hot dog and earn 16 K. So 15 K is a better price. Sample input \#5: Output: Sample input \#6: Output: Hint: Use the .split() method to convert each input line into a list of words. Then you can convert each word to an integer. Output: Explanation: At a price of 2 K Alfons will sell 3 hot dogs and earn 6 K. At a price of 1000 K he will sell 1 hot dog and earn 1000 K. Sample input \#4: Output: Explanation: Alfons will have the same revenue at a price of 10 K or 30 K, and 10 K is the lower of these prices. Use the .split() method to convert each input line into a list of words. Then you can convert each word to an integer. Various solutions are possible. As one possible approach, as you read the input, build a list that for every possible price P (from 1 to 5000 ) stores the number of customers whose maximum price is P. In another possible approach you can read all input prices into a list, then sort it. (The built-in sort() method will run in O(NlogN ).) With either of these approaches, you can then compute the revenue at every possible price by making a single pass over the resulting list, without any nested loop. The test cases have as many as 20,000 input numbers. Your program must run in at most 0.2 seconds on each test case, so it will need to be efficient in order to pass all tests. If your program runs in O(N2), where N is either the input length or the number of possible prices, it will be too slow. Beware of nested loops

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions