熟练写码的版友来解决一个算24问题
版主: verdelite, TheMatrix
-
- 论坛支柱
2024年度优秀版主
TheMatrix 的博客 - 帖子互动: 271
- 帖子: 13564
- 注册时间: 2022年 7月 26日 00:35
Re: 熟练写码的版友来解决一个算24问题
Python code 来了:
from fractions import Fraction from itertools import permutations operators=[["(","+",")" ],["(","-",")"],["(","*",")"],["Fraction(",",",")"]] def ProcessApair(y): return([list(x[0:i])+[operators[j][0]+x[ i ]+operators[j][1]+x[i+1]+operators[j][2]]+list(x[i+2:len(x)]) for x in y for i in range(len(x)-1) for j in range(len(operators))]) def evaluate(x): try: return eval(x) except ZeroDivisionError: pass y=permutations([str(i) for i in [3,3,7,7]]) results=list(set([item[0] for item in ProcessApair(ProcessApair(ProcessApair(y))) if evaluate(item[0])==24 or evaluate(item[0])==Fraction(24,1)])) for item in results: print(item)这儿有online python console可以直接跑,
https://www.programiz.com/python-progra ... -compiler/
3,3,7,7的结果:
(7*(3+Fraction(3,7)))
((3+Fraction(3,7))*7)
(7*(Fraction(3,7)+3))
((Fraction(3,7)+3)*7)
3,3,8,8的结果:
Fraction(8,(3-Fraction(8,3)))
1,5,5,5的结果:
(5*(5-Fraction(1,5)))
((5-Fraction(1,5))*5)
2,7,8,10的结果:
解不出来。
1,7,8,10的结果:(里面很多重复的,没想好怎么reduce,LOL。现在只去除了字面的重复。)
((10-(7*1))*8)
(((7+8)-1)+10)
(8+(7+(10-1)))
(((8+10)-1)+7)
((7+8)+(10-1))
((7+(10+8))-1)
((1*8)*(10-7))
(((7-1)+8)+10)
((10+7)+(8-1))
(((10+7)+8)-1)
((10-(1-7))+8)
((8+(10+7))-1)
(8-(1-(10+7)))
((7-1)+(10+8))
(7-(1-(8+10)))
(((7+10)-1)+8)
((10+(8-1))+7)
(((10+8)-1)+7)
((10+8)-(1-7))
(((10*1)-7)*8)
(8-((1-10)-7))
(10-((1-7)-8))
((10-7)*(8*1))
((10+(7+8))-1)
((7-(1-8))+10)
(7+((8-1)+10))
(((8-1)+10)+7)
(((10+7)-1)+8)
((8+(7-1))+10)
(10-((1-8)-7))
((10-1)+(7+8))
((8*1)*(10-7))
(8+((7+10)-1))
((8+(7+10))-1)
(10+(8+(7-1)))
(((8+7)+10)-1)
(8*((10*1)-7))
((8+10)+(7-1))
(8*((1*10)-7))
(10-(1-(8+7)))
((10+(7-1))+8)
((10-(1-8))+7)
(8*((10-7)*1))
(8+((10-1)+7))
(((8+7)-1)+10)
((10+7)-(1-8))
(Fraction(8,1)*(10-7))
(Fraction((10-7),1)*8)
((10-7)*(1*8))
(((1*10)-7)*8)
Fraction(((10-7)*8),1)
((7+(8+10))-1)
(10+(7+(8-1)))
(10+((8+7)-1))
(8+(10-(1-7)))
(8*(1*(10-7)))
((10-7)*Fraction(8,1))
(7+(8-(1-10)))
((8+(10-1))+7)
((10-1)+(8+7))
Fraction((10-7),Fraction(1,8))
((7-1)+(8+10))
(7+(10+(8-1)))
((10+8)+(7-1))
(7-(1-(10+8)))
(7+((10+8)-1))
(7+(10-(1-8)))
Fraction((8*(10-7)),1)
(((10-7)*1)*8)
(8+(10+(7-1)))
((8*(10-7))*1)
(((7-1)+10)+8)
((8-1)+(7+10))
(((7+8)+10)-1)
(8*(10-(7*1)))
(8*(10-(1*7)))
(10+(8-(1-7)))
(8+((10+7)-1))
((1*(10-7))*8)
((7+10)+(8-1))
(((10+8)+7)-1)
(8-((1-7)-10))
(8*(Fraction(10,1)-7))
(7-((1-8)-10))
((8+7)-(1-10))
(8*Fraction((10-7),1))
(10+((8-1)+7))
(8+((7-1)+10))
(((10-1)+8)+7)
(10+(7-(1-8)))
(10+((7-1)+8))
((8+7)+(10-1))
((7-(1-10))+8)
((7+8)-(1-10))
((8-1)+(10+7))
(((10-7)*8)*1)
(10-(1-(7+8)))
((8+10)-(1-7))
(7-((1-10)-8))
((Fraction(10,1)-7)*8)
(1*(8*(10-7)))
(1*((10-7)*8))
(7+((10-1)+8))
(8*(10-Fraction(7,1)))
((7+(10-1))+8)
(((7+10)+8)-1)
(8-(1-(7+10)))
Fraction(8,Fraction(1,(10-7)))
((7+(8-1))+10)
((7+10)-(1-8))
(7+(8+(10-1)))
(7+((8+10)-1))
(10+((7+8)-1))
(((8-1)+7)+10)
((10-Fraction(7,1))*8)
(8+(7-(1-10)))
(((10-1)+7)+8)
((8-(1-7))+10)
(((8+10)+7)-1)
((8-(1-10))+7)
((10-(1*7))*8)
((10+(8+7))-1)
Re: 熟练写码的版友来解决一个算24问题
1,7,8,10显然不是我要的。
verdelite 写了: 2023年 1月 22日 14:41 Python code 来了:
from fractions import Fraction from itertools import permutations operators=[["(","+",")" ],["(","-",")"],["(","*",")"],["Fraction(",",",")"]] def ProcessApair(y): return([list(x[0:i])+[operators[j][0]+x[ i ]+operators[j][1]+x[i+1]+operators[j][2]]+list(x[i+2:len(x)]) for x in y for i in range(len(x)-1) for j in range(len(operators))]) def evaluate(x): try: return eval(x) except ZeroDivisionError: pass y=permutations([str(i) for i in [3,3,7,7]]) results=list(set([item[0] for item in ProcessApair(ProcessApair(ProcessApair(y))) if evaluate(item[0])==24 or evaluate(item[0])==Fraction(24,1)])) for item in results: print(item)这儿有online python console可以直接跑,
[url]https://www.programiz.com/python-progra ... -compiler/[\url]
3,3,7,7的结果:
(7*(3+Fraction(3,7)))
((3+Fraction(3,7))*7)
(7*(Fraction(3,7)+3))
((Fraction(3,7)+3)*7)
3,3,8,8的结果:
Fraction(8,(3-Fraction(8,3)))
1,5,5,5的结果:
(5*(5-Fraction(1,5)))
((5-Fraction(1,5))*5)
2,7,8,10的结果:
解不出来。
1,7,8,10的结果:(里面很多重复的,没想好怎么reduce,LOL。现在只去除了字面的重复。)
((10-(7*1))*8)
(((7+8)-1)+10)
(8+(7+(10-1)))
(((8+10)-1)+7)
((7+8)+(10-1))
((7+(10+8))-1)
((1*8)*(10-7))
(((7-1)+8)+10)
((10+7)+(8-1))
(((10+7)+8)-1)
((10-(1-7))+8)
((8+(10+7))-1)
(8-(1-(10+7)))
((7-1)+(10+8))
(7-(1-(8+10)))
(((7+10)-1)+8)
((10+(8-1))+7)
(((10+8)-1)+7)
((10+8)-(1-7))
(((10*1)-7)*8)
(8-((1-10)-7))
(10-((1-7)-8))
((10-7)*(8*1))
((10+(7+8))-1)
((7-(1-8))+10)
(7+((8-1)+10))
(((8-1)+10)+7)
(((10+7)-1)+8)
((8+(7-1))+10)
(10-((1-8)-7))
((10-1)+(7+8))
((8*1)*(10-7))
(8+((7+10)-1))
((8+(7+10))-1)
(10+(8+(7-1)))
(((8+7)+10)-1)
(8*((10*1)-7))
((8+10)+(7-1))
(8*((1*10)-7))
(10-(1-(8+7)))
((10+(7-1))+8)
((10-(1-8))+7)
(8*((10-7)*1))
(8+((10-1)+7))
(((8+7)-1)+10)
((10+7)-(1-8))
(Fraction(8,1)*(10-7))
(Fraction((10-7),1)*8)
((10-7)*(1*8))
(((1*10)-7)*8)
Fraction(((10-7)*8),1)
((7+(8+10))-1)
(10+(7+(8-1)))
(10+((8+7)-1))
(8+(10-(1-7)))
(8*(1*(10-7)))
((10-7)*Fraction(8,1))
(7+(8-(1-10)))
((8+(10-1))+7)
((10-1)+(8+7))
Fraction((10-7),Fraction(1,8))
((7-1)+(8+10))
(7+(10+(8-1)))
((10+8)+(7-1))
(7-(1-(10+8)))
(7+((10+8)-1))
(7+(10-(1-8)))
Fraction((8*(10-7)),1)
(((10-7)*1)*8)
(8+(10+(7-1)))
((8*(10-7))*1)
(((7-1)+10)+8)
((8-1)+(7+10))
(((7+8)+10)-1)
(8*(10-(7*1)))
(8*(10-(1*7)))
(10+(8-(1-7)))
(8+((10+7)-1))
((1*(10-7))*8)
((7+10)+(8-1))
(((10+8)+7)-1)
(8-((1-7)-10))
(8*(Fraction(10,1)-7))
(7-((1-8)-10))
((8+7)-(1-10))
(8*Fraction((10-7),1))
(10+((8-1)+7))
(8+((7-1)+10))
(((10-1)+8)+7)
(10+(7-(1-8)))
(10+((7-1)+8))
((8+7)+(10-1))
((7-(1-10))+8)
((7+8)-(1-10))
((8-1)+(10+7))
(((10-7)*8)*1)
(10-(1-(7+8)))
((8+10)-(1-7))
(7-((1-10)-8))
((Fraction(10,1)-7)*8)
(1*(8*(10-7)))
(1*((10-7)*8))
(7+((10-1)+8))
(8*(10-Fraction(7,1)))
((7+(10-1))+8)
(((7+10)+8)-1)
(8-(1-(7+10)))
Fraction(8,Fraction(1,(10-7)))
((7+(8-1))+10)
((7+10)-(1-8))
(7+(8+(10-1)))
(7+((8+10)-1))
(10+((7+8)-1))
(((8-1)+7)+10)
((10-Fraction(7,1))*8)
(8+(7-(1-10)))
(((10-1)+7)+8)
((8-(1-7))+10)
(((8+10)+7)-1)
((8-(1-10))+7)
((10-(1*7))*8)
((10+(8+7))-1)