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.

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

Gribouillis
1,391
Programming Explorer
Team Colleague

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

Schol-R-LEA
1,438
Commie Mutant Traitor
Featured Poster

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.

vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague

`9x^6+2x^4+2x+5`

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

vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.

Broken Link

**You're trying to visit a URL that doesn't currently exist on the web.**
Most likely, a member posted a link a long time ago to a web page that has since been removed.
It's also possible that there was a typo when posting the URL.
We redirect you to this notice instead of stripping out the link to preserve the integrity of the post.