Answer to Question #300671 in Python for Nikhitha

Question #300671

Can you explain the clear code for polynomial expression in python and add two polynomial expression in python

1
Expert's answer
2022-02-21T16:47:42-0500
def DispPoly(C,n):
    C = C[::-1]
    s = ""
    k=n-1
    for r in range(0,len(C)-2):
        if(r==0):
            if(C[r]>0): s = s + str(C[r])+"x^"+str(k)
            if(C[r]<0): s = s + str(C[r])+"x^"+str(k)
        if(r>0):
            if(C[r]>0): s = s + "+" + str(C[r])+"x^"+str(k)
            if(C[r]<0): s = s + str(C[r])+"x^"+str(k)
            
        k=k-1


    if(C[n-2]>0): s = s + "+" + str(C[n-2]) + "x"
    if(C[n-2]<0): s = s +       str(C[n-2]) + "x"


    if(C[n-1]>0): s = s + "+" + str(C[n-1])
    if(C[n-1]<0): s = s +       str(C[n-1])
    print(s)    
    
N = int(input("Enter the Order of Polynomial: "))
N = N+1
print("Enter coefficients of the two polynomiuals separated by SPACE: ")
Coeff=[]
AddP=[]
for r in range(0,N):
    s = "Enter coeff. of x^" + str(r) + str(": ")
    t = (str(input(s))).split(" ")
    temp = []
    temp.append(int(t[0]))
    temp.append(int(t[1]))
    Coeff.append(temp)
    AddP.append(sum(temp))
    
Coeff = np.array(Coeff)
Coeff=np.transpose(Coeff)
print("\nPolynomial-1:")
DispPoly(Coeff[0],N)
print("\nPolynomial-2:")
DispPoly(Coeff[1],N)
print("\nAfter Adding two polynomials: ")
DispPoly(AddP,N)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS