In [7]:
import operator;
nGlobalList = list(range(1,7));
print(nGlobalList)
opStrings = ["+","-","/","*"]
def minus(a,b):
return a-b;
ops = [operator.add, minus,operator.truediv,operator.mul]
nOPs = len(ops)
In [ ]:
stringList = [str(i) for i in nGlobalList];
resList = [];
computeBinaryOperator(nGlobalList,stringList,"",resList)
print(len(resList))
In [6]:
nCombs = 0;
def computeBinaryOperator(nList,nStringList, depthString, resList ):
depthString+=" "
listSize = len(nList);
if(listSize == 1):
print(nStringList[0], "result: ",nList[0])
resList.append((nStringList[0],nList[0]));
#print(depthString,nStringList,nList)
for i in range(0,listSize):
#choose first number in 1:listSize-1
x = nList[i];
xStr = nStringList[i];
for j in range(1,listSize):
#choose second number
k = (j+i)%listSize;
y = nList[k];
yStr = nStringList[k];
# apply operator
for oIdx in range(0,nOPs):
try:
res = ops[oIdx](x,y); # result is always at beginning of the new list
except:
res = float("Inf");
resString = "(" + xStr + opStrings[oIdx] + yStr +")";
# call recursion with new list (otherwise call by reference,notg good
nListNew = nList.copy();
nStringListNew = nStringList.copy();
#Overwrite choosen x
nListNew[i] = res; # put result at position i
nStringListNew[i] = resString;
# remove choosen element y
nListNew.pop(k); nStringListNew.pop(k);
computeBinaryOperator(nListNew,nStringListNew,depthString,resList);
In [4]:
nCombs = 0;
def computeBinaryOperator2(nList,resList):
listSize = len(nList);
if(listSize == 1):
resList.append(nList[0]);
#print(depthString,nStringList,nList)
for i in range(0,listSize):
#choose first number in 1:listSize-1
x = nList[i];
for j in range(1,listSize):
#choose second number
k = (j+i)%listSize;
y = nList[k];
# apply operator
for oIdx in range(0,nOPs):
try:
res = ops[oIdx](x,y); # result is always at beginning of the new list
except:
res = float("Inf");
# call recursion with new list (otherwise call by reference,notg good
nListNew = nList.copy();
#Overwrite choosen x
nListNew[i] = res; # put result at position i
# remove choosen element y
nListNew.pop(k);
computeBinaryOperator2(nListNew,resList);
In [6]:
resList = [];
computeBinaryOperator2(nGlobalList,resList)
print(len(resList))
In [ ]: