# Compute every combination of operations written in a .TXT file

Your task is to create a program that computes every combination of the operations written in a .TXT file.

Example TXT File:

1
+
2
/
3

Example Output:

1+2/3=1
1+3/2=2
2+1/3=1
2+3/1=5
3+2/1=5
3+1/2=2
1/2+3= //See bonuses. If your program does not feature float, leave blank result.
1/3+2= //See above.
2/1+3=5
2/3+1= //See above.
3/2+1= //See above.
3/1+2=5

Specs:

• Every line of the .TXT file must contain a number or an operation;

• The minimum length for the .TXT file is 5 lines;

• The operations must be computed from left to right. E.g. 4+4/2 results in 4;

• Your program should feature +, -, * and / operations;

• Your program should work with any valid input of any valid length.

Scoring:

Code length is your starting score, but not the only parameter. Here's a short list of bonuses you can achieve to lower your score. The user with the lowest score wins.

• Implement ^: -20;

• Implement log (in base e): -30;

• Implement logn (where n is any positive base): -50 (only -20 if you achieved the log bonus);

• Implement e (and the number in the line below is the exponent of 10. So 1e4=10000): -20;

• Can handle negative values: -10;

• Can handle float values: -20;

• There are no digits (0123456789) in the code: -10;

• There is no call to eval or equivalent: -20;

• All of the above: -100.

For a total of -250 bonus points.

Good luck!

• Why the The operations must be computed in order. E.g. 4+4/2 results 4; spec? This is against the rules of math! – ProgramFOX Jan 14 '14 at 17:54
• I was using an old calculator when I had this idea. In old calculators you had to type a number, press the desired operation, then type another number then press the desired operation again, and so on. There were no parenthesis or rules. I tried to give this challenge this behavior (also, the .TXT file is - more or less - what those calculators printed on paper :) ) – Vereos Jan 14 '14 at 17:59
• That was probably put there in order to generate all possible combinations. If you allow math precedence rules, then both 4/2+4 and 4+4/2 generate the same value, and that would be pointless. – Tobia Jan 14 '14 at 18:00
• @Tobia That is indeed a nice side-effect that convinced me to post this :P With larger inputs with lots of * this effect is even more appreciable, like in 4*2+4*2*3! – Vereos Jan 14 '14 at 18:04
• implementing ^ is extremely easy; it is just the XOR of two numbers! (If you meant power, please say so. Other people might get confused) – Justin Jan 15 '14 at 5:28

# Mathematica (-4 = 246 - 250)

Update: operation e instead of the number representation with e.

i = "1.txt";

w@"log"=r=Real;
e_~p~c_:=(g[x_r,w@c=c]~g~y_r:=#;p)&@e
y~Log~x~p[x+y,"+"][x-y,"-"][x/y,"/"][x*y,"*"][x*10^y,"e"][x^y,"^"]~"logn";
x_r~g~r:=Log@x
w@y_:=N@ToExpression@y
g[,x_]:=x
f@x_r:=s~Print~x

File:

1
+
2
/
3

Output:

1+2/3=1.
1+3/2=2.
1/2+3=3.5
1/3+2=2.33333
2+1/3=1.
2+3/1=5.
2/1+3=5.
2/3+1=1.66667
3+1/2=2.
3+2/1=5.
3/1+2=5.
3/2+1=2.5

File:

-1
^
2
e
3
logn
4
log

Output:

-1e2^4logn3log=2.81943
-1e2^4loglogn3=2.65196
-1e3^2logn4log=2.29916
-1e3^2loglogn4=1.89411
-1e3^4logn2log=3.68545
-1e3^4loglogn2=4.78822
-1e4^2logn3log=2.81943
-1e4^2loglogn3=2.65196
2^-1e3logn4log=1.50027
2^-1e3loglogn4=1.31783
2^-1e4logn3log=2.04804
2^-1e4loglogn3=1.94981
2^3logn4e-1log=-1.89712
2^3logn4loge-1=0.0405465
2^3loge-1logn4=-1.13287
2^3loglogn4e-1=0.0528098
2^4e-1logn3log=-0.849063
2^4e-1loglogn3=-0.687244
2^4logn3e-1log=-1.37685
2^4logn3loge-1=0.0925734
2^4loge-1logn3=-1.16766
2^4loglogn3e-1=0.0928245
2e-1logn3^4log=1.52735
2e-1log^4logn3=1.73268
2e3logn4^-1log=-1.70163
2e3logn4log^-1=0.587671
2e3log^-1logn4=-1.46309
2e3loglogn4^-1=0.683487
2e4logn3^-1log=-2.19884
2e4logn3log^-1=0.454785
2e4log^-1logn3=-2.08708
2e4loglogn3^-1=0.479139
2logn3^-1e4log=9.6709
2logn3^-1loge4=4605.61
2logn3^4e-1log=-4.14483
2logn3^4loge-1=-0.184224
2logn3e-1^4log=-11.0526
2logn3e-1log^4=58.2928
2logn3e4^-1log=-8.74978
2logn3e4log^-1=0.114289
2logn3log^-1e4=-21712.7
2logn3log^4e-1=0.00449933
2logn3loge-1^4=4.49933*10^-6
2logn3loge4^-1=-0.000217127
2logn4^-1e3log=7.6009
2logn4^-1loge3=693.147
2logn4^3e-1log=-4.38203
2logn4^3loge-1=-0.207944
2logn4e-1^3log=-8.9872
2logn4e-1log^3=-26.8849
2logn4e3^-1log=-6.21461
2logn4e3log^-1=0.160911
2logn4log^-1e3=-1442.7
2logn4log^3e-1=-0.0333025
2logn4loge-1^3=-0.000333025
2logn4loge3^-1=-0.0014427
2log^-1e3logn4=5.24728
2log^-1e4logn3=8.71723
2log^-1logn3e4=3336.14
2log^-1logn4e3=264.383
2log^3e-1logn4=-2.45411
2log^3logn4e-1=-0.079315
2log^4e-1logn3=-3.43036
2log^4logn3e-1=-0.133446
2loge-1^3logn4=-5.77604
2loge-1^4logn3=-9.71807
2loge-1logn3^4=34.8402
2loge-1logn4^3=-7.13719
2loge3^-1logn4=-4.71851
2loge3logn4^-1=0.211931
2loge4^-1logn3=-8.05
2loge4logn3^-1=0.124224
2loglogn3^-1e4=-29974.7
2loglogn3^4e-1=0.00123874
2loglogn3e-1^4=1.23874*10^-6
2loglogn3e4^-1=-0.000299747
2loglogn4^-1e3=-3782.39
2loglogn4^3e-1=-0.001848
2loglogn4e-1^3=-0.00001848
2loglogn4e3^-1=-0.00378239
3^-1e2logn4log=0.928001
3^-1e2loglogn4=0.905028
3^-1e4logn2log=2.45982
3^-1e4loglogn2=3.02001
3^2logn4e-1log=-1.84202
3^2logn4loge-1=0.0460561
3^2loge-1logn4=-1.09312
3^2loglogn4e-1=0.0567841
3^4e-1logn2log=1.10457
3^4e-1loglogn2=1.06479
3^4logn2e-1log=-0.45573
3^4logn2loge-1=0.184686
3^4loge-1logn2=-1.18625
3^4loglogn2e-1=0.213568
3e-1logn2^4log=2.20856
3e-1logn4^2log=-0.282015
3e-1log^2logn4=0.267803
3e-1log^4logn2=1.07121
3e2logn4^-1log=-1.4145
3e2logn4log^-1=0.706966
3e2log^-1logn4=-1.25596
3e2loglogn4^-1=0.796204
3e4logn2^-1log=-2.69953
3e4logn2log^-1=0.370435
3e4log^-1logn2=-3.36583
3e4loglogn2^-1=0.297104
3logn2^-1e4log=8.74978
3logn2^-1loge4=-4605.61
3logn2^4e-1log=-0.460342
3logn2^4loge-1=0.184224
3logn2e-1^4log=-7.3681
3logn2e-1log^4=11.5128
3logn2e4^-1log=-9.6709
3logn2e4log^-1=0.103403
3logn2log^-1e4=21712.7
3logn2log^4e-1=0.00449933
3logn2loge-1^4=4.49933*10^-6
3logn2loge4^-1=0.000217127
3logn4^-1e2log=4.83776
3logn4^-1loge2=23.2586
3logn4^2e-1log=-2.76776
3logn4^2loge-1=-0.0465173
3logn4e-1^2log=-5.07034
3logn4e-1log^2=6.42709
3logn4e2^-1log=-4.37258
3logn4e2log^-1=0.228698
3logn4log^-1e2=-429.948
3logn4log^2e-1=0.00540964
3logn4loge-1^2=0.000540964
3logn4loge2^-1=-0.0429948
3log^-1e2logn4=3.25409
3log^-1e4logn2=13.152
3log^-1logn2e4=-1356.82
3log^-1logn4e2=-6.78412
3log^2e-1logn4=-1.52528
3log^2logn4e-1=0.0135682
3log^4e-1logn2=-2.7792
3log^4logn2e-1=0.0542729
3loge-1^2logn4=-3.18625
3loge-1^4logn2=-12.745
3loge-1logn2^4=103.066
3loge-1logn4^2=2.53804
3loge2^-1logn4=-3.38977
3loge2logn4^-1=0.295005
3loge4^-1logn2=-13.4234
3loge4logn2^-1=0.0744968
3loglogn2^-1e4=73701.6
3loglogn2^4e-1=0.0000338917
3loglogn2e-1^4=3.38917*10^-8
3loglogn2e4^-1=0.000737016
3loglogn4^-1e2=1474.03
3loglogn4^2e-1=0.000460242
3loglogn4e-1^2=0.0000460242
3loglogn4e2^-1=0.147403
4^-1e2logn3log=1.07498
4^-1e2loglogn3=1.0641
4^-1e3logn2log=2.07516
4^-1e3loglogn2=2.46505
4^2e-1logn3log=-0.849063
4^2e-1loglogn3=-0.687244
4^2logn3e-1log=-1.37685
4^2logn3loge-1=0.0925734
4^2loge-1logn3=-1.16766
4^2loglogn3e-1=0.0928245
4^3e-1logn2log=0.985097
4^3e-1loglogn2=0.892428
4^3logn2e-1log=-0.510826
4^3logn2loge-1=0.179176
4^3loge-1logn2=-1.26573
4^3loglogn2e-1=0.20562
4e-1logn3^2log=-0.362939
4e-1log^2logn3=-0.159149
4e2logn3^-1log=-1.69629
4e2logn3log^-1=0.589523
4e2log^-1logn3=-1.62963
4e2loglogn3^-1=0.613635
4e3logn2^-1log=-2.48205
4e3logn2log^-1=0.402893
4e3log^-1logn2=-3.05208
4e3loglogn2^-1=0.327646
4logn2^-1e3log=6.21461
4logn2^-1loge3=-693.147
4logn2^3e-1log=-0.223144
4logn2^3loge-1=0.207944
4logn2e-1^3log=-4.82831
4logn2e-1log^3=-4.16891
4logn2e3^-1log=-7.6009
4logn2e3log^-1=0.131563
4logn2log^-1e3=1442.7
4logn2log^3e-1=0.0333025
4logn2loge-1^3=0.000333025
4logn2loge3^-1=0.0014427
4logn3^-1e2log=4.37258
4logn3^-1loge2=-23.2586
4logn3^2e-1log=-1.83741
4logn3^2loge-1=0.0465173
4logn3e-1^2log=-4.14
4logn3e-1log^2=4.28489
4logn3e2^-1log=-4.83776
4logn3e2log^-1=0.206707
4logn3log^-1e2=429.948
4logn3log^2e-1=0.00540964
4logn3loge-1^2=0.000540964
4logn3loge2^-1=0.0429948
4log^-1e2logn3=3.89449
4log^-1e3logn2=9.49455
4log^-1logn2e3=-471.234
4log^-1logn3e2=-29.7315
4log^2e-1logn3=-1.50127
4log^2logn3e-1=0.0594631
4log^3e-1logn2=-1.90823
4log^3logn2e-1=0.14137
4loge-1^2logn3=-3.59718
4loge-1^3logn2=-8.55208
4loge-1logn2^3=-23.1661
4loge-1logn3^2=3.23492
4loge2^-1logn3=-4.48912
4loge2logn3^-1=0.222761
4loge3^-1logn2=-10.437
4loge3logn2^-1=0.0958128
4loglogn2^-1e3=2122.09
4loglogn2^3e-1=0.0104643
4loglogn2e-1^3=0.000104643
4loglogn2e3^-1=0.00212209
4loglogn3^-1e2=336.343
4loglogn3^2e-1=0.00883964
4loglogn3e-1^2=0.000883964
4loglogn3e2^-1=0.0336343

Description:

• The function w[y] converts string y to the number if y is not an operation.
• The pattern g[g[x,c],y] is converted to x+y, x-y, etc depending on the operation c. Otherwise it stay a symbolic expression.
• f[x] prints x only if it is a number.
• That doesn't appear to read from the text file. So, this doesn't complete the challenge. – Rees Jan 15 '14 at 8:19
• @Rees Updated. Thank you for pointing me out! – ybeltukov Jan 15 '14 at 8:38
• You are using ToExpression which is the equivalent of eval (and you may not be evaluating log, only logn, if I'm not mistaken.) Either way, I don't think you can claim the full -250 bonus. – Tobia Jan 15 '14 at 22:16
• @Tobia ToExpression is the sole documented way for converting a string with one number to a number. I don't use ability ToExpression to convert expressions. log take only one argument so it is implemented separately, see w@"log"=r=Real; and x_r~g~r:=Log@x lines. I think that I implemented all bonus tasks! – ybeltukov Jan 15 '14 at 22:46
• Fair then. Good job! – Tobia Jan 15 '14 at 23:11

## GolfScript 21 (51 characters -10 for handling negatives -20 for handling ^)

Explanation:

Suppose a.txt contains "2+3/1*4" (separated by new lines).

'^'/'?'* # Replace all '^' with '?' to handle exponent.  This works by splitting the string on '^' and then folding on '?'.
"\n"/ # Split on newlines, so that we have an array of characters
(20@ # Pull out the first element of the array, push a 20 (ASCII value for space) and rearrange stack

At this point, the stack will look like 2 20 '+3/1*4'.

2/ # Group by 2

So the stack looks like 2 20 [['+','3'],['/','1'],['*','4']].

{(}% # Map each element of the array to the tail of the tuple followed by its head.

So the stack looks like 2 20 [['3'], '+', ['1'], '/', ['4'], '*']

+''++ # Concatenate, stringify, and concatenate.  Note that adding '' to an array flattens it.

So we are left with '2 3 + 1 / 4 *'.

~ # Finally, evaluate the string, since we were left with valid GolfScript.

This works with negatives, but accomplishes none of the other bonuses.

• this doesn't output all possible combinations, does it? – Doorknob Jan 14 '14 at 22:18
• It doesn't seem to. – Tobia Jan 14 '14 at 22:44

### GolfScript, score 54 (104 characters, bonus for neg., ^, e)

n%[[]]\{n/{{^2$<\@^>++}+\:^,),/}+%}/.&{2/1\{0='+-*/^e'\?0<&}/},{?}:^;{10\?*}:e;{."="+[n]@+2/{~~\~}/n}/ The input is given on STDIN. Example For input 1 2 3 + e: 3+2e1=50 3e2+1=301 2+3e1=50 2e3+1=2001 2+1e3=3000 2e1+3=23 3+1e2=400 3e1+2=32 1+3e2=400 1e3+2=1002 1+2e3=3000 1e2+3=103 Commented code with basic building blocks # split input n% # generate all permutations of the input (I'm working on a shorter implementation) [[]]\{n/{{^2$<\@^>++}+\:^,),/}+%}/

# remove any duplicates (occur if same number/operator is given more than once)
.&

# filter for those permutations where each second element is an operator
{2/1\{0='+-*/^e'\?0<&}/},

# define operators ^ and e
{?}:^;
{10\?*}:e;

# Loop over all permutations
{
.
# make string representation
"="+
# reorder expression from ltr-infix into stack order and execute operations
[n]@+2/{~~\~}/
n
}/

Javascript, 338 characters

Handles e, negative values, and floats, bonus -50, score 288.

function g(a,p,r){a.length<1?r.push(p):a.map(function(c){g(a.filter(function(x){return x!=c}),p.concat(c),r)})}
l.map(function(y){k.map(function(x){r=x[0];s=r;y.map(function(b,i){n=b+x[i+1];s=s+n;r=eval(r+n);});print(s+'='+r)})})

Example here.

# Ruby 177 characters

• Implemented ^: -20
• Handles negative values: -10
• No eval: -20

### Total: 127

$<.map{|x|x=~/\d+/&&x.to_i||x.chomp}.permutation{|x|$_=x*'';a,*b=x;puts$_+"=#{b.each_slice(2).reduce(a){|m,(o,y)|[m+y,m-y,m*y,m/y,m**y]["+-*/^".index o]}}"if~/^-?\d+(\W-?\d+)*$/}

Run with the input file as command line argument or give input on stdin.

$<.map{|x|x=~/\d+/?x.to_i: x.chomp} .permutation{|x|$_=x*''
a,*b=x
puts $_+"=#{ b.each_slice(2).reduce(a){|m,(o,y)|[m+y,m-y,m*y,m/y,m**y]["+-*/^".index o]} }" if ~/^-?\d+(\W-?\d+)*$/}

# APL, score 36

156 chars - 20 (^) - 50 (logn) - 20 (e) - 10 (negative) - 20 (float) = 36

{⍺←{⍵[{1≥⍴⍵:⊃,↓⍵⋄⊃⍪/⍵,∘∇¨⍵∘~¨⍵}⍳⍴⍵]}⋄e←{⍺×10*⍵}⋄⍪,(↓⍺v/⍨~d)∘.{(,⍵,⍪⍺,'='),{⍎⍕⍵,⍺}/⌽(⍎↑⍵),↓'+-×÷*⍟e'['+-*/^le'⍳∊1⌷¨⍺],⍪1↓⍵}↓⍺v/⍨d←2|⍳⍴v←('\S+'⎕S'&')⍵⎕ntie 0}

Without any instructions to the contrary, I made logn an infix operator, as you would type it on a calculator, so that log2(16) is 2 logn 16.

Source

⍝ main function; ⍵ is the file to read
{
p←{1≥⍴⍵:⊃,↓⍵⋄⊃⍪/⍵,∘∇¨⍵∘~¨⍵}   ⍝ permutation matrix (taken from "dfns")
q←{⍵[p⍳⍴⍵]}                   ⍝ all permutations of the given vector
e←{⍺×10*⍵}                    ⍝ 'e' function as required
v←('\S+'⎕S'&')⍵⎕ntie 0        ⍝ read input file and split on spaces
d←2|⍳⍴v                       ⍝ bitmap of odd indices of input vector
n←v/⍨d                        ⍝ odd-index values: the numbers
o←v/⍨~d                       ⍝ even-index values: the operators

⍝ f will be called for each permutation of operators and numbers;
⍝ ⍺ is the (shuffled) list of operators; ⍵ of numbers
f←{
⍝ translate the operators into APL
⍝ a trick is used to recognize 'logn' by its first letter only
t←'+-×÷*⍟e'['+-*/^le'⍳∊1⌷¨⍺]

⍝ prepare the list of operations to perform, in reverse order,
⍝ followed by a single scalar with the value of the first number
m←⌽(⍎↑⍵),↓t,⍪1↓⍵

⍝ fold over the previous vector, executing every operation in order
r←{⍎⍕⍵,⍺}/m

⍝ return the result, preceded by a representation of the operation
(,⍵,⍪⍺,'='),r
}
⍝ generate all permutations of operators and of numbers; call f on
⍝ every combination (outer product); ravel and tabulate the result
⍪,(↓q o)∘.f↓q n
}

Examples

Input file:

-2.5
/
3
^
4

Output:

{paste golfed code here} 'a.txt'
-2.5  /  3  ^  4 = 0.4822530863
-2.5  /  4  ^  3 = ¯0.244140625
3  /  -2.5  ^  4 = 2.0736
3  /  4  ^  -2.5 = 2.052800957
4  /  -2.5  ^  3 = ¯4.096
4  /  3  ^  -2.5 = 0.4871392899
-2.5  ^  3  /  4 = ¯3.90625
-2.5  ^  4  /  3 = 13.02083333
3  ^  -2.5  /  4 = 0.01603750748
3  ^  4  /  -2.5 = ¯32.4
4  ^  -2.5  /  3 = 0.01041666667
4  ^  3  /  -2.5 = ¯25.6

Input file:

2.5
e
3
logn
4

Output:

{paste golfed code here} 'b.txt'
2.5  e  3  logn  4 = 0.1771838201
2.5  e  4  logn  3 = 0.1084874404
3  e  2.5  logn  4 = 0.2022289117
3  e  4  logn  2.5 = 0.08888300898
4  e  2.5  logn  3 = 0.1538078748
4  e  3  logn  2.5 = 0.1104756749
2.5  logn  3  e  4 = 11989.77847
2.5  logn  4  e  3 = 1512.941595
3  logn  2.5  e  4 = 8340.437671
3  logn  4  e  2.5 = 399.0350129
4  logn  2.5  e  3 = 660.9640474
4  logn  3  e  2.5 = 250.6045754