Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a code which reads a text file containing polynomials f1, f2, ..., fk and takes an input value x0 from a user and calculates

image text in transcribed Write a code which reads a text file containing polynomials f1, f2, ..., fk and takes an input value x0 from a user and calculates the values f1(x0), f2(x0), ..., fk(x0) and writes these values to the same text file according to the format shown in the following: x_2=2 should be inside of the code input= input1.txt f1(x): +3x^2 + 2x^1 -5x^0 f2(x): -5x^3 + 1x^1 +7x^0 f3(x): +4x^4 - 2x^0 output: f1(x): +3x^2 + 2x^1 -5x^0 f2(x): -5x^3 + 1x^1 +7x^0 f3(x): +4x^4 - 2x^0 f1(2): 11 f2(2): -31 f3(2): 62 input1.txt image text in transcribed my code : image text in transcribed can u correct my code and please check the result i cant send any other question in chegg thank you

def evaluate_polynomial(polynomial, x): terms = polynomial.split(" ") coefficients = [] for term in terms: if "x^" in term: coefficient, exponent = term.split("x^", 1) else: coefficient = term exponent = "0" if coefficient[-1].isnumeric(): coefficients.append((float(coefficient), int(exponent))) result = 0 for coefficient, exponent in coefficients: result += coefficient * pow(x, exponent) return result

with open("input1.txt", "r") as f: polynomials = f.readlines()

x=2

with open("input1.txt", "a") as f: for polynomial in polynomials: if "f" in polynomial: polynomial = polynomial[3:] result = evaluate_polynomial(polynomial, x) f.write(f" f({x}) = {result} ")

4. Write a code which reads a text file containing polynomials f1,f2,,fk and takes an input value x0 from a user and calculates the values f1(x0),f2(x0),,fk(x0) and writes these values to the same text file according to the format shown in the following: Sample input and output Note 1: There is at least one space between the terms of each polynomial fi(x) in the input text file. Use these spaces for splitting purposes. Note 2: You are allowed to use Python's pow () function when calculating fi(x0). f1(x):+3x2+2x15x f2(x):5x3+1x1+7x f3(x):+4x42x

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 Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions

Question

2 What can organisations do to improve employee utilisation?

Answered: 1 week ago

Question

4 When is it a good idea to use the external supply of labour?

Answered: 1 week ago

Question

3. What would you do now if you were Mel Fisher?

Answered: 1 week ago