A symmetric polynomial is a polynomial which is unchanged under permutation of its variables.

In other words, a polynomial f(x,y) is symmetric if and only if f(x,y) = f(y,x); a polynomial g(x,y,z) is symmetric iff g(x,y,z) = g(x,z,y) = g(y,x,z) = etc.

For example, x^2+2xy+y^2, xy and x^3+x^2y+xy^2+y^3 are symmetric polynomials, where 2x+y and x^2+y are not.

The challenge

You will be given a polynomial, and your program should output truthy/falsy values, depending on if the given polynomial is a symmetric polynomial.

The input format is allowed in two ways. A string, and an array, like ["x^2","2xy","y^2"], where the polynomial is the sum of each elements.


x^2+2xy+y^2 => true
xy => true
xy+yz+xz-3xyz => true
(x+y)(x-y) => false
2x+y => false
x^2+y => false
x+2y+3 => false


The operation has orders, just like in normal math. the order is like this:

() => ^ => * => +-

rules apply.

All the characters in the alphabet (a~z) are accepted as variables, everything else are numbers.

The given polynomial will have 2 or more variables.

Multiplication does not require the * operator, You only need to detect juxtaposition. (detecting by juxtaposition is not neccessary, use the better option)

  • \$\begingroup\$ Is a function a valid input? \$\endgroup\$ – Pavel Feb 4 '17 at 5:36
  • 2
    \$\begingroup\$ Please specify the allowed inputs exactly and include test cases that cover the space well. \$\endgroup\$ – xnor Feb 4 '17 at 7:45
  • 2
    \$\begingroup\$ I don´t think its fair to ask someone to spend time writing an answer and then you decide if it´s valid. So I agree with @xnor. Also you indicated () => ^ => */ => +- but your examples do not show all these. I would have imagined we could expect - but not /. As you have mentioned () are we expected to handle in the format (-1+x)(-y-3)? \$\endgroup\$ – Level River St Feb 4 '17 at 10:43
  • 2
    \$\begingroup\$ This is still unclear. Can arbitrary degree polynomials be multiplied? Can operations be done to exponents? Where can minus signs appear? \$\endgroup\$ – xnor Feb 4 '17 at 18:35
  • 1
    \$\begingroup\$ "The input format is allowed in two ways" but they give two very different challenges, and a lot of the "spec" section is irrelevant if the second one if chosen. \$\endgroup\$ – Peter Taylor Feb 6 '17 at 17:49

Maxima, 40 bytes


Try It Online!

A function that takes a polynomial as input and returns true if it is symmetric else returns false


Mathematica, 43 bytes


Unnamed function taking as input a polynomial in the given format (except that juxtaposed variables must be separated by a space) and returning True or False. Variables@# detects the variables appearing in the input (and thus the input can contain all kinds of weird variable names, not just single letters). SymmetricReduction returns an ordered pair of polynomials, where the first one is symmetric and the two sum to the original polynomial; therefore we can detect whether the input is symmetric by seeing whether the second polynomial is identically 0.

  • \$\begingroup\$ According to OP, you take input as a function, which could maybe let you golf that more. \$\endgroup\$ – Pavel Feb 4 '17 at 5:40

TI-Basic, 46 bytes

Basically, how this works is the polynomial is entered into a (two-byte) variable type that is dynamically evaluated. Then, we swap the X and Y values enough times to see if the function is symmetric.

Prompt u
If u=W
  • \$\begingroup\$ I know the spec is shaky, so: this program takes input as a string, works with either juxtaposition or the * operator, and outputs 1 for true and 0 for false. \$\endgroup\$ – Timtech Feb 10 '17 at 12:00
  • \$\begingroup\$ Sure, that should be enough to detect one. \$\endgroup\$ – Matthew Roh Feb 11 '17 at 12:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.