Humans are a remarkable species, but we can be very awkward to understand sometimes—especially for computers. In particular, we seem to like writing polynomials in a very convoluted fashion with seemingly arbitrary rules.
What is the shortest program you can write to format a polynomial correctly using these rules?
A list of integers between -1000 and 1000 (inclusive), representing the coefficients of a polynomial, with the last entry being the coefficient of x^0 (the constant), the second last being the coefficient of x^1, etc.
A string representing this polynomial in the correctly formatted mathematical notation of humans.
- The sign on the leading coefficient is only shown if it is negative.
- Components with coefficient of 0 are not printed (except for the corner case where all coefficients are 0*).
+1are to be displayed without the 1, unless they are the constant.
- The exponent is only shown if it is greater than 1 and the variable is only shown if the exponent is greater than 0.
- *Corner case: while zero values usually result in not printing that component, if all coefficients are zero the constant 0 should be printed.
- This is code-golf so the winner will be the program with the fewest bytes.
Example Input and Output
Input: Output:  0 [0,0] 0 [0,-1,35,0] -x^2+35x [5,1,7,-9] 5x^3+x^2+7x-9 [100,0,0,-1] 100x^3-1 [931,21,-11,1] 931x^3+21x^2-11x+1
I look forward to seeing your solutions. Have fun!
- You can surround operations by whitespace if you wish. So
3x + 5are both fine.
3x +5are not.
- If you want to produce actual exponent characters (say in Tex) that is allowed as it is even closer to how humans write.
- Coefficients must appear without any decimals e.g.
3x^2 + 4versus
xreplacement doesn't change