24點解題和出題工具
接著上一篇文章

24點解題工具代碼:
import itertools
#by jason
def calculate24(nums):
? ? permutations = list(itertools.permutations(nums))
? ? operators = list(itertools.product(["+", "-", "*", "/"], repeat=3))
? ? for perm in permutations:
? ? ? ? for operator in operators:
? ? ? ? ? ? equation = f"{perm[0]} {operator[0]} {perm[1]} {operator[1]} {perm[2]} {operator[2]} {perm[3]}"
? ? ? ? ? ? if abs(eval(equation) - 24) < 1e-6:
? ? ? ? ? ? ? ? return equation.replace("//", "/")
? ? return "無解"
while 1:
????a=int(input("1:"))
????b=int(input("2:"))
????c=int(input("3:"))
????d=int(input("4:"))
????print(calculate24([a, b, c, d]))
????input("_____________________________________")

24點出題工具代碼:
#by jason
import itertools
import random as r
def py24(nums):
? ? permutations = list(itertools.permutations(nums))
? ? operators = list(itertools.product(["+", "-", "*", "/"], repeat=3))
? ? for perm in permutations:
? ? ? ? for operator in operators:
? ? ? ? ? ? equation = f"{perm[0]} {operator[0]} {perm[1]} {operator[1]} {perm[2]} {operator[2]} {perm[3]}"
? ? ? ? ? ? if abs(eval(equation) - 24) < 1e-6:
? ? ? ? ? ? ? ? return equation.replace("//", "/")
? ? return "No"
while 1:
? ? a=r.randint(1,10)
? ? b=r.randint(1,10)
? ? c=r.randint(1,10)
? ? d=r.randint(1,10)
? ? f=[a,b,c,d]
? ? if((py24(f))!="No"):
? ? ? ? print(a," ",b," ",c," ",d,' ')
? ? ? ? input("")
? ? ? ? print(py24(f))
? ? ? ? input("")
? ? ? ? print("___________________________________________________________________")
? ? else:
? ? ? ? pass