Polynomial in Python

Problem Statement:

We need to take an integer(n) as input, then next to n lines, there will be two separated integers representing Pi and Ci of a polynomial. We need to represent those Pi and Ci in polynomial form.

For example,
Input
Enter number of terms in polynomial: 5
1 2
2 3
3 4
0 9
7 6

Output
6x^7 + 4x^3 + 3x^2 + 2x^1 + 9x^0

Code for Polynomial in Python:

n = int(input('Enter number of terms in polynomial: '))
Pi = []
Ci = []
for i in range(n):
    pi_ci = input().split()
    Pi.append(pi_ci[0])
    Ci.append(pi_ci[1])
poly = []
for i in range(n):
    poly.append(Ci[i] + 'x^'+Pi[i])
Pi.sort(reverse=True)
newPoly = []
for i in range(n):
    for j in range(n):
        if Pi[i]==poly[j][3]:
            newPoly.append(poly[j])
print(' + '.join(newPoly))

Output:

Output for Polynomial in Python

Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you