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.

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.

This article has been dead for over six months. Start a new discussion instead.