26
1
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.
5Welcome to PPCG! Nice first challenge! I think this would attract more answers if it were the other way around (find the set given the expression) because it seems like this is quite a tricky challenge. Good challenge nonetheless! – HyperNeutrino – 2017-04-17T03:05:19.870
Welcome again! Adding on to @HyperNeutrino, there will probably be multiple solutions to some of the sets, which could be a problem, when deciding which question is the "best" unless the deciding factor is the conciseness – David Archibald – 2017-04-17T03:07:03.267
@HyperNeutrino Thanks! I did apprehend this might turn to be a bit hard, but I fully believe in the superior capabilities of the golfers here; let's see how it turns out. :) – Ankoganit – 2017-04-17T03:08:06.910
3Yes. Some of the golfers on this site have amazing superpowers, and we even suspect some are golfing bots >_> :D – HyperNeutrino – 2017-04-17T03:09:50.723
@DavidArchibald Yes, the intended output is any solution that works. – Ankoganit – 2017-04-17T03:10:12.100
Is the format
.5
OK in the output? – xnor – 2017-04-17T05:16:15.130@xnor Yep, it's fine. – Ankoganit – 2017-04-17T05:17:46.157
Well it turns out that the golfing people here were had golfing superpowers and got this challenge in a few hours >_> lol I still don't get how it works – HyperNeutrino – 2017-04-17T20:02:51.477
0.0 The fact that the first three answers were all different methods to do this in Haskell has me worried. – Delioth – 2017-04-17T20:17:58.787