38
1
Introduction
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?
Challenge
Input
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.
Output
A string representing this polynomial in the correctly formatted mathematical notation of humans.
Rules:
- The sign on the leading coefficient is only shown if it is negative.
Right: -x^2+3
Wrong: +x^2+3
- Components with coefficient of 0 are not printed (except for the corner case where all coefficients are 0*).
Right: x^5-x^2+3
Wrong: x^5+0x^4+0x^3-x^2+0x+3
- Coefficients
-1
and+1
are to be displayed without the 1, unless they are the constant.
Right: x^5-x^2+1
Wrong: 1x^5-1x^2+1
- The exponent is only shown if it is greater than 1 and the variable is only shown if the exponent is greater than 0.
Right: 3x^3-7x^2+2x+1
Wrong: 3x^3-7x^2+2x^1+1x^0
- *Corner case: while zero values usually result in not printing that component, if all coefficients are zero the constant 0 should be printed.
Right: 0
Wrong: 0x+0
Wrong: (nothing)
- 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
[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!
EDIT:
- You can surround operations by whitespace if you wish. So
3x+5
and3x + 5
are both fine.3x+ 5
and3x +5
are 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.
9x^2
is correct,9.0x^2
is not.
7A question which I should have asked in the sandbox but didn't, can we print with whitespace between the operators? So
3x^2 + 4
versus3x^2+4
? – Giuseppe – 2018-03-15T14:08:53.9571Do we need to output exponents using carets? Or would producing an actual superscript character be allowed (e.g. for an answer in TeX)? – Tutleman – 2018-03-15T15:12:05.017
Can we take the input as strings instead of integers? – Manish Kundu – 2018-03-15T15:18:42.900
Are we allowed to output
5.0x^3+x^2+7.0x-9.0
instead of5x^3+x^2+7x-9
? – Kevin Cruijssen – 2018-03-15T16:13:31.8433@KevinCruijssen I'm not the OP but I would say not, because most humans don't write like that. – ShreevatsaR – 2018-03-15T16:39:27.283
@Giuseppe Thank you! Fixed now. And yes you can have whitespace surrounding operators. Thanks for the question. – Oisín Moran – 2018-03-15T16:52:52.400
@Tutleman Yes, as that is closer to human behaviour. Thanks for the question! – Oisín Moran – 2018-03-15T16:54:54.500
2@ManishKundu Yeah sure you can take the input as strings. – Oisín Moran – 2018-03-15T16:56:18.983
@KevinCruijssen Nope but thanks for the question! – Oisín Moran – 2018-03-15T16:56:53.053
Suggestion: Inputs should also be allowed to be taken in the order of increasing powers, i.e, first x^0, then x^1 and so on... – Manish Kundu – 2018-03-15T17:19:33.440
@ManishKundu No, I'm just going stick with this order to avoid ambiguity and keep a single standard. – Oisín Moran – 2018-03-15T23:14:14.123
@DLosc
[931, 21, -11, 1] -> 931x^3+21x^2-11x+1
added to the description as well. I'm expecting something very funky from you now. – Oisín Moran – 2018-03-15T23:16:15.1431@OisínMoran Nothing funky, just making sure a simple
1x
->x
replacement doesn't change21x^2
into2x^2
. – DLosc – 2018-03-16T05:22:03.363May we also optionally take the length of the array? – Giuseppe – 2018-03-16T12:16:21.197
@Giuseppe I think I'll keep it as is but thanks for the suggestion! – Oisín Moran – 2018-03-17T13:00:26.520
What is the expected behaviour if the function is given an empty array? Also, if we're not allowed to pass the array length to the function, is a sentinel allowed for languages that don't pass array bounds to functions (i.e. C)? – ErikF – 2018-03-17T21:52:35.610