Using our familiar mathematical symbols: +, x, parenthesis, and any rational number, it's easy to create expressions that evaluates to some desired number. For example: 1+(2x3)=7
, (1+2)+(3x6.5)=22.5
and so on. Boring enough.
In this challenge, we'll use a new operator: ±
. The use of ±
in an expression means you need to evaluate the expression by replacing the ±
's by +
or -
in all possible ways, and return the set of all possible values. For example:
1±2±3 = {-4,0,2,6}
because1±2±3
can be any of1+2+3
,1+2-3
,1-2+3
and1-2-3
and their values are6,0,2,-4
respectively.(±2)x(2±3) = {-10,-2,2,10}
for similar reasons.
Now, as it turns out, given any set of distinct real numbers, it's possible to create an expression with +
,x
,(
,)
,±
, and real numbers that evaluates to the given set.
Task
Your task is to write a program or function in a language of your choice, that takes a sequence (list/array/any convenient format) of integers and outputs an expression (as a string) consisting of +
,x
,(
,)
,±
, and rational numbers that evaluates to the set of the given numbers.
- Note that the exact character
±
doesn't matter; you can use any other character of your choice as long as it's distinguishable from the other characters you're using. But you must mention which character you are using in your submission. - The input is allowed to consist of decimal approximations (up to reasonable accuracy) of the rational numbers used.
- Input and output can be taken in any of the standard ways.
- Standard loopholes are forbidden.
- You can assume the given integers will be distinct, and provided in increasing order.
- Output may contain spaces and newlines.
Winning Criterion
This is code-golf, so shortest code in bytes wins.
Examples
Input | Possible output -------------+----------------------------- [1,2,3] | 2±0.5±0.5 [-7,-3,1,21] | (1±2)x(3±4)
Idea taken from a question in the Tournament of Towns, Fall 2015.