Back
Close

Expand the polynomial

Statement

 Goal

You are given a polynomial that is partially or fully factorized and you have to write a code that fully expands it.
For example, if you are given (x-1)*(x+2)=x²+x-2, its coefficients that are 1 1 -2 and you have to write x^2+x-2.
Input
A partially or not factorized polynomial.
Output
The expanded polynomial written in the standard way:
* x^1 is written x
* 1x^3 is written x^3
* 0x^2 and x^0 are not written
Constraints
All the coefficients are integers (positive, null or negative).
All coefficients are in decreasing order: (x^3 then x^2 then x^1 then x^0)
Example
Input
(x-1)*(x+2)
Output
x^2+x-2

Tags
MathematicsAlgebraParsingOutput formatting

Difficulty
Hard

Test cases
Simple product Test
Input
(x-1)*(x+2)
Output
x^2+x-2

Simple product Validator
Input
(x+1)*(x-2)
Output
x^2-x-2

Square Test
Input
(x-2)^2
Output
x^2-4x+4

Square Validator
Input
(x+3)^2
Output
x^2+6x+9

With ax Test
Input
(2x+3)(x-2)
Output
2x^2-x-6

With ax Validator
Input
(2x-5)(3x+7)
Output
6x^2-x-35

Null coef Test
Input
(x-2)(x+2)
Output
x^2-4

Null coef Validator
Input
(2x-3)(2x+3)
Output
4x^2-9

Bigger degree Test
Input
(4x^3-5x^2+7x-1)(4x^2-x+1)
Output
16x^5-24x^4+37x^3-16x^2+8x-1

Bigger degree Validator
Input
(x^2+2x+3)(x^3+2x^2+3x+4)
Output
x^5+4x^4+10x^3+16x^2+17x+12

Cyclotomic polynomial Test
Input
(x-1)(x+1)(x^2+1)
Output
x^4-1

Cyclotomic polynomial Validator
Input
(x-1)(x+1)(x^2+x+1)(x^2-x+1)
Output
x^6-1

Monomial with no integer Test
Input
(2x^2+x-3)(x^2-x-2)
Output
2x^4-x^3-8x^2+x+6

Monomial with no integer Validator
Input
(2x^2-x-3)(x^2+x-2)
Output
2x^4+x^3-8x^2-x+6

Solution language

Solution

Stub generator input