10x^6+3x^5+4x^+4
9x^6+2x^4+2x+5

1)could you please propose a suitable data structure to store above ploynomials.
2) and, how to add & multiply them using python codes.

## Recommended Answers

It looks like homework. Did you try to add the polynomials in a python console ?

## All 4 Replies

It looks like homework. Did you try to add the polynomials in a python console ?

As Gribouilis said, there is only so much we can suggest without doing the work for you. As for data structures, there are three options I can recommend, in order from least preferred to most:

• Use a list consisting of lists of exponent:coefficient pairs (you want the exponent first because the exponents are unique, while the coefficients can be repeated). This is simple and easy to define, but not very structured. Also, if the polynomial is in more than one variable, you would need multiple lists representing each variable.
• Use a dictionary of dictionaries with the exponent as the key and the coefficient as the value for the inner dictionaries, and the variable as the key for the outer dictionary. This is a bit better structured, and makes it somewhat easier to handle reduction when performing arithmetic, but it would still be a rather problematic.
• Define a class, to represent each exponent:coefficient pair, and a dictionary of with the variables as the keys and a list of the objects as the value. This is the most structured approach, but also the most complex.

I would probably choose version two, as it probably would be the easiest for handling the arithmetic. I would wrap that representation in a `Polynomial` class to make the arithmetic operations part of the whole structure.

`9x^6+2x^4+2x+5`
would be ...
`9*x**6 + 2*x**4 + 2*x + 5`

You could store it as a string and design a little parser to pick it appart.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.