Skip to content

Instantly share code, notes, and snippets.

@yingtai
Created May 9, 2011 07:03
Show Gist options
  • Save yingtai/962179 to your computer and use it in GitHub Desktop.
Save yingtai/962179 to your computer and use it in GitHub Desktop.
#coding:utf-8
'''
使用可能なオペランドは2~6個
main(operandList,answer[,n])
operandList : 被演算子のlist
answer : 答え
n : n則で計算(デフォルト値4,許容値1~6)
'''
comb=[[[0,1]],
[[0,1],[0,2],[1,2]],
[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]],
[[0,1],[0,2],[0,3],[0,4],[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]],
[[0,1],[0,2],[0,3],[0,4],[0,5],[1,2],[1,3],[1,4],[1,5],[2,3],[2,4],[2,5],[3,4],[3,5],[4,5]]
]
def delol(L):
for i in range(len(L)):
L[i]=tuple(L[i])
L=list(set(L))
for j in range(len(L)):
L[j]=list(L[j])
L=list(L)
return L
def solve(opes,ans,n):
length=len(opes)
for i in range(len(opes)):
opes[i]=float(opes[i])
ans=float(ans)
_opes=opes[:]
if not any([n==1,n==2,n==3,n==4,n==5,n==6]):
return False
if not any([length==2,length==3,length==4,length==5,length==6]):
return False
turns=[]
for i1 in range((length-1)*(length)/2):
if length-2>0:
for i2 in range((length-2)*(length-1)/2):
if length-3>0:
for i3 in range((length-3)*(length-2)/2):
if length-4>0:
for i4 in range((length-4)*(length-3)/2):
if length-5>0:
for i5 in range((length-5)*(length-4)/2):
turns.append([i1,i2,i3,i4,i5])
else:
turns.append([i1,i2,i3,i4])
else:
turns.append([i1,i2,i3])
else:
turns.append([i1,i2])
else:
turns.append([i1])
if n==3:
n=4
elif n==4:
n=6
elif n==5:
n=8
elif n==6:
n=10
funclist=[(lambda x,y:x+y),(lambda x,y:x*y),(lambda x,y:x-y),(lambda x,y:y-x),(lambda x,y:x/y),(lambda x,y:y/x),
(lambda x,y:x**y),(lambda x,y:y**x),(lambda x,y:x**(1/y)),(lambda x,y:y**(1/x))]
funcs=[]
for j1 in range(n):
for j2 in range(n):
for j3 in range(n):
for j4 in range(n):
for j5 in range(n):
funcs.append([j1,j2,j3,j4,j5])
if len(funcs[0])>length-1:
for i in range(len(funcs)):
for j in range(len(funcs[i])-(length-1)):
funcs[i].pop()
funcs=delol(funcs)
for func in funcs:
for turn in turns:
opes=_opes[:]
try:
for i in range(length-1):
opes[comb[length-(i+2)][turn[i]][0]] = funclist[func[i]](opes[comb[length-(i+2)][turn[i]][0]],opes[comb[length-(i+2)][turn[i]][1]])
opes.pop(comb[length-(i+2)][turn[i]][1])
except (ZeroDivisionError,OverflowError,ValueError):
opes=[ans-1]
if opes==[ans]:
return [_opes,turn,func]
return False
def data2string(data):
if data!=False:
funcchr=['+','*','-','-','/','/','^','^','√','√']
opes=data[0];turn=data[1];func=data[2]
length=len(opes)
for i in range(length):
if opes[i]==float(int(opes[i])):
opes[i]=str(int(opes[i]))
else:
opes[i]=str(opes[i])
for i in range(length-1):
if any([func[i]==3,func[i]==5,func[i]==7,func[i]==8]):
opes[comb[length-(i+2)][turn[i]][0]]='('+opes[comb[length-(i+2)][turn[i]][1]]+funcchr[func[i]]+opes[comb[length-(i+2)][turn[i]][0]]+')'
opes.pop(comb[length-(i+2)][turn[i]][1])
else:
opes[comb[length-(i+2)][turn[i]][0]]='('+opes[comb[length-(i+2)][turn[i]][0]]+funcchr[func[i]]+opes[comb[length-(i+2)][turn[i]][1]]+')'
opes.pop(comb[length-(i+2)][turn[i]][1])
return opes[0]
else:
return 'Not found'
def main(opes,ans,n=4):
return data2string(solve(opes,ans,n))
#test
print main([1,1,2,3,5],8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment