Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Explain me on how to go about this problem and check for all test cases 2 2. FISHING TOWNS (4 MARKS) Write a program, stored
Explain me on how to go about this problem and check for all test cases
2 2. FISHING TOWNS (4 MARKS) Write a program, stored in a file named fish.py, that performs the following task. The program prompts the user to input a file name. If there is no file with that name in the working directory, then the program outputs an (arbitrary) error message and exits. The contents of the file consists of some number of lines, each line being a sequence of two strictly positive integers separated by at least one space, with possibly spaces before and after the first and second number, respectively, the first numbers listed from the first line to the last line forming a strictly increasing sequence. The first number represents the distance (say in kilometres) from a point on the coast to a fishing town further down the coast (so the towns are listed as if we were driving down the coast from some fixed point); the second number represents the quantity (say in kilos) of fish that has been caught during the early hours of the day by that town's fishermen. For instance, the contents of the file coast_1.txt can be displayed as 5 70 15 100 1200 20 which corresponds to the case where we have 3 towns, one situated 5 km south the point, a second one situated 15 km south the point, and a third one situated 1200 km south the point, with 70, 100 and 20 kilos of fish being caught by those town's fishermen, respectively. The aim is to maximise the quantity of fish available in all towns (the same in all towns) by possibly transporting fish from one town to another one, but unfortunately losing 1 kilo of fish per kilometre. For instance, if one decides to send 20 kilos of fish from the second town to the first one, then the second town ends up having 100-20-80 kilos of fish, whereas the first one ends up having 70+20-(15-5)=80 kilos of fish too. The program outputs that maximum quantity of fish that all towns can have by possibly transporting fish in an optimal way. Here is a possible interaction: $ cat coast 1.txt 5 70 15 100 1200 20 $python3 fish.py Which data file do you want to use? coast_1.txt The maximum quantity of fish that each town can have is 20. $ cat coast_2.txt 20 300 40 400 340 700 360 600 $ python3 fish.py Which data file do you want to use? coast_2.txt The maximum quantity of fish that each town can have is 415. You can assume that the contents of any test file is as expected, you do not have to check that it is as expected.
Step by Step Solution
★★★★★
3.32 Rating (164 Votes )
There are 3 Steps involved in it
Step: 1
The following is a Python program to solve the fishing towns problem Python def readfilefilename Rea...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