Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Please write in Python) In her bike rides around Warsaw, Jill happened upon a shop that sold interesting glass bottles. She thought it might make

(Please write in Python)

In her bike rides around Warsaw, Jill happened upon a shop that sold interesting glass bottles. She thought it might make an interesting project to use such bottles for measuring liquids, but this would require placing markings on the bottles to indicate various volumes. Where should those volume marks be placed?

Jill formalized the problem as follows. Assume a bottle is formed by revolving a shape that is the same as the graph of a polynomial P

between x=xlow and x=xhigh around the x-axis. Thus the x-axis is coincident with a vertical line through the center of the bottle. The bottom of the bottle is formed by a solid circular region at x=xlow, and the top of the bottle, at x=xhigh

, is left open.

The first sample input represents a bottle formed using the simple polynomial 40.25x

, with xlow=0 and xhigh=12. The bottom of this bottle is a circle with a radius of 4, and the opening at the top is a circle with a radius of 1. The height of this bottle is 12. Volume markings are in increments of 25

.

Given a polynomial P

, xlow, xhigh, and the volume increment between successive marks on the bottle, compute the distances up from xlow for the marks at successive volume increments. A mark cannot be made past the top of the bottle, and no more than the first 8 increments should be marked. Assume the value of P is greater than zero everywhere between xlow and xhigh

.

Input

Each test case consists of three lines of bottle data:

Line 1: n

, the degree of the polynomial (an integer satisfying 0n10

).

Line 2: a0

, a1, , an, the real coefficients of the polynomial P defining the bottles shape, where a0 is the constant term, a1 is the coefficient of x1, , and an is the coefficient of xn. For each i, 100ai100, and an0

.

Line 3:

xlow

and xhigh, the real valued boundaries of the bottle (100xlow<xhigh100 and xhighxlow>0.1

).

inc

, an integer which is the volume increment before each successive mark on the bottle (1inc500

).

Output

For each test case, display the case number and the volume of the full bottle on one line. On a second line, display the increasing sequence of no more than 8

successive distances up from the bottom of the bottle for the volume markings. All volumes and height marks should be given with exactly two decimal places. If the bottle does not have a volume that allows at least one mark, display the phrase insufficient volume. No test case will result in a mark within 0.01 from the top of the bottle. The volume of the bottle will not exceed 1000. All rounded distances for marks on a bottle differ by at least 0.05.

Sample Input 1

1 4.0 -0.25 0.0 12.0 25 1 4.0 -0.25 0.0 12.0 300 0 1.7841241161782 5.0 10.0 20 0 1.0 0.0 10.0 10 

Sample Output 1

Case 1: 263.89 0.51 1.06 1.66 2.31 3.02 3.83 4.75 5.87 Case 2: 263.89 insufficient volume Case 3: 50.00 2.00 4.00 Case 4: 31.42 3.18 6.37 9.55 

pseudcode:

#! /usr/bin/python3 def prettydata(data): #get the data for input, returns a list of lists of len 3; [[deg, [coeffs], [start, stop, increment]],...] def binsearch(p, targ, lo, lo2, hi): #Binary search mid = (lo2 + hi)/2 while (the difference btwn hi and lo2 exceeds the tolerance): mid = (hi + lo2)/2 att = (volume of the bottle generated by p (= poly**2 already) from lo to mid , 'att' ~ 'attempt' if att < targ: #change lo2 to mid elif att > targ: #change hi to mid else: #Should you hit it dead on... return mid return mid def planarintegral(p, x0): #p is a list of the coefficients of the polynomial, x0 is the point of evaluation (will use for endpoints of bottle) integral = 0 for k in range(len(p)): integral += #iterate over the powers in the polynomial (include constant, it is subtracted out in the 'vol' function below) return integral def vol(p, lo, hi): #returns the volume of bottle given by the polynomial p (will be f(x)**2) via the Fundamental Theorem of Calculus from math import pi return pi*(value of planar integral) def eachcase(data, case): #data is [deg, [coefs], [start, stop, increment]] poly = data[1] #So think of the polynomial as its coefficients; p(x) = a0 + a1*x + a2*x**2 + ... -> represented as [a0, a1, a2,...] lo, hi, incr = data[2] deg = #get degree of polynomial poly2 = [0]*(2*deg + 1) #Think of poly2 as poly**2, you'll need to square the polynomial to form the volume integral L = len(poly) #Take the Cauchy Product to square the polynomial (write coeffs out by hand and group them together to see the pattern) nested for-loops: poly2[i + j] += poly[i]*poly[j] #(vol of revolution of a shape f(x) is pi*integral from (start point) to (end point) of (f(x)**2) dx, and we're finding f(x)**2 = f(x)*f(x)) nteg = #list of length = 2*(deg + 1), 'nteg' ~ 'IN-TEGrand', make all the elements the same integer for simplicity for k in range(len(poly2)): nteg[k + 1] = #power rule for poly**2, remember, there will be no constant term (hence nteg[k+1] to skip nteg[0] = constant term) total = vol(nteg, lo, hi) print('Case %s: %s' % (case, format(total, '.2f'))) if (expression for insufficent volume): print('insufficient volume') else: #marknum is the number of marks on the bottle or 8 marknum = min(int(total/incr), 8) x2 = lo for-loop: targ = (k + 1)*increment #use binsearch to set x2 = vol at whatever increment #Since formatting matters and you can't end a line in 2 spaces (which is visually okay), you have to end the line the usual way. if k != marknum - 1: print(format(x2 - lo, '.2f'), end = ' ') else: print(format(x2 - lo, '.2f')) def main(): data = prettydata() #data is [deg, [coefs], [start, stop, increment]] case = 1 while data != []: dataline = data.pop() eachcase(dataline, case) case += 1 main() 

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

Step: 3

blur-text-image

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

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions