itertools.permutations (a,b) ,a为列表,b为组合个数,以a中的元素取出b个,类似于阶乘,如itertools.permutations([‘+’, ‘-’, ‘*’, ‘/’], 3) ,取三个运算符的组合,第一位取了加号,第二位是除了加号之外的另三个运算符,第三位是除第一、二位之外的剩下的两个运算符。而itertools.product([列表], repeat=3),取出列表中3个元素,取过的可以重复再取。如列表中有10个元素,那么将是10的三次方。
detailAnswer中生成1~13扑克牌的24点,每次生成计算时间比较长,不如将生成的组合打印出来放到列表中,每次抽取出作为现成的问题。列表中的列表会有重复,进行排序之后放入临时列表中,可以去除重复的组合。
import itertools
import random
def booleanResult(nums):
for num_seq in itertools.permutations(nums):
for ops_seq in itertools.product(['+', '-', '*', '/'], repeat=3):
# 对于每个组合,计算表达式的值,并判断是否等于24
if eval(f'{num_seq[0]} {ops_seq[0]} {num_seq[1]} {ops_seq[1]} {num_seq[2]} {ops_seq[2]} {num_seq[3]}') ==24: # 由于浮点数计算的精度限制,将等于24的浮点数结果视为相等
print(f"解法:{num_seq[0]} {ops_seq[0]} {num_seq[1]} {ops_seq[1]} {num_seq[2]} {ops_seq[2]} {num_seq[3]}")
return None
# 测试用例
nums =[2, 5, 9, 7]
booleanResult(nums)
def detailAnswer(number):
# questions = [[1, 2, 3, 4], [1, 2, 3, 7], [1, 2, 4, 12], [1, 2, 5, 13], [1, 2, 6, 8], [1, 2, 6, 11], [1, 2, 6, 12], [1, 2, 7, 9], [1, 2, 7, 10], [1, 2, 7, 8], [1, 2, 8, 13], [1, 2, 5, 9], [1, 2, 6, 9], [1, 2, 9, 12], [1, 2, 9, 13], [1, 2, 3, 10], [1, 2, 4, 10], [1, 2, 10, 11], [1, 2, 10, 12], [1, 2, 12, 13], [1, 2, 3, 13], [1, 3, 4, 5], [1, 3, 4, 11], [1, 3, 4, 12], [1, 3, 5, 8], [1, 3, 5, 9], [1, 3, 5, 6], [1, 3, 6, 12], [1, 3, 7, 13], [1, 3, 8, 9], [1, 3, 8, 12], [1, 3, 8, 13], [1, 3, 4, 9], [1, 3, 9, 11], [1, 3, 9, 12], [1, 3, 6, 10], [1, 3, 7, 10], [1, 3, 10, 11], [1, 3, 12, 13], [1, 4, 6, 13], [1, 4, 5, 7], [1, 4, 7, 12], [1, 4, 7, 13], [1, 4, 8, 9], [1, 4, 8, 11], [1, 4, 8, 12], [1, 4, 9, 10], [1, 4, 9, 11], [1, 4, 9, 12], [1, 4, 9, 13], [1, 5, 6, 7], [1, 5, 6, 12], [1, 5, 6, 13], [1, 5, 7, 11], [1, 5, 7, 12], [1, 5, 8, 10], [1, 5, 8, 11], [1, 5, 9, 10], [1, 5, 10, 12], [1, 6, 7, 10], [1, 6, 7, 11], [1, 6, 8, 9], [1, 6, 8, 10], [1, 7, 8, 9], [1, 2, 10, 13], [1, 2, 11, 12], [2, 3, 4, 9], [2, 3, 4, 10], [2, 3, 5, 6], [2, 3, 5, 7], [2, 3, 5, 13], [2, 3, 4, 6], [2, 3, 6, 12], [2, 3, 6, 13], [2, 3, 7, 11], [2, 3, 7, 12], [2, 3, 8, 10], [2, 3, 8, 11], [2, 3, 5, 9], [2, 3, 9, 10], [2, 3, 12, 13], [2, 3, 4, 13], [2, 4, 5, 11], [2, 4, 5, 13], [2, 4, 6, 8], [2, 4, 6, 10], [2, 4, 6, 12], [2, 4, 6, 7], [2, 4, 7, 9], [2, 4, 7, 11], [2, 4, 8, 10], [2, 3, 5, 11], [2, 4, 5, 10], [2, 5, 6, 8], [2, 5, 6, 11], [2, 5, 7, 10], [2, 5, 7, 13], [2, 5, 8, 9], [2, 5, 6, 10], [1, 2, 6, 13], [2, 3, 6, 9], [2, 3, 6, 11], [2, 5, 6, 7], [2, 6, 7, 9], [2, 6, 8, 9], [1, 2, 7, 11], [2, 3, 7, 13], [1, 2, 8, 9], [2, 3, 5, 8], [2, 4, 8, 11], [2, 4, 8, 12], [2, 5, 8, 13], [2, 4, 9, 10], [2, 5, 9, 11], [2, 6, 9, 12], [2, 7, 9, 13], [1, 2, 5, 10], [2, 3, 7, 10], [2, 5, 9, 10], [2, 5, 10, 11], [2, 7, 10, 11], [2, 8, 10, 12], [2, 9, 10, 13], [2, 3, 10, 12], [1, 2, 3, 11], [2, 4, 6, 11], [2, 5, 7, 11], [2, 6, 8, 11], [2, 6, 11, 12], [2, 7, 9, 11], [2, 8, 10, 11], [2, 10, 11, 12], [2, 4, 6, 13], [2, 6, 8, 13], [2, 4, 8, 13], [2, 8, 10, 13], [2, 9, 11, 13], [2, 5, 10, 13], [2, 10, 12, 13], [2, 6, 12, 13], [1, 3, 9, 13], [1, 3, 10, 12], [2, 3, 8, 12], [2, 3, 10, 13], [2, 3, 11, 12], [1, 3, 4, 13], [3, 4, 5, 7], [3, 4, 5, 10], [3, 4, 5, 12], [3, 4, 6, 11], [3, 4, 6, 12], [3, 4, 7, 10], [3, 4, 8, 9], [3, 4, 8, 11], [3, 4, 12, 13], [1, 3, 5, 10], [3, 4, 5, 13], [3, 5, 6, 9], [3, 5, 6, 10], [3, 5, 7, 9], [1, 3, 6, 7], [2, 3, 6, 7], [2, 3, 6, 8], [3, 4, 6, 10], [3, 5, 6, 11], [3, 6, 7, 8], [3, 6, 7, 13], [3, 6, 9, 12], [1, 3, 4, 7], [3, 4, 7, 12], [3, 5, 7, 8], [3, 6, 7, 9], [3, 7, 8, 11], [3, 7, 9, 12], [3, 7, 10, 13], [1, 2, 3, 9], [3, 4, 7, 9], [3, 5, 8, 9], [3, 7, 9, 10], [3, 8, 9, 11], [3, 9, 10, 13], [3, 4, 9, 12], [3, 5, 10, 11], [3, 6, 10, 12], [1, 3, 8, 11], [3, 4, 5, 11], [3, 4, 11, 13], [1, 3, 11, 12], [3, 4, 8, 12], [3, 5, 7, 12], [3, 5, 10, 13], [3, 6, 9, 13], [3, 7, 8, 13], [1, 4, 8, 13], [1, 4, 10, 11], [2, 4, 7, 10], [2, 4, 6, 9], [2, 4, 9, 13], [2, 4, 10, 12], [3, 4, 10, 13], [3, 4, 11, 12], [2, 4, 5, 6], [2, 4, 5, 8], [4, 5, 6, 9], [4, 5, 6, 10], [4, 5, 7, 8], [4, 5, 7, 11], [4, 5, 8, 12], [4, 5, 9, 13], [4, 5, 10, 12], [4, 5, 12, 13], [4, 5, 7, 9], [4, 6, 7, 10], [2, 4, 7, 8], [4, 7, 8, 12], [4, 7, 9, 13], [1, 4, 7, 8], [3, 4, 5, 8], [4, 5, 8, 13], [4, 8, 9, 12], [4, 5, 10, 11], [4, 6, 10, 12], [4, 7, 9, 10], [2, 4, 10, 11], [4, 7, 11, 13], [4, 8, 11, 12], [4, 11, 12, 13], [1, 5, 7, 13], [1, 5, 8, 12], [1, 5, 9, 11], [2, 5, 6, 9], [2, 5, 9, 12], [2, 5, 6, 12], [3, 5, 9, 13], [3, 5, 10, 12], [4, 5, 10, 13], [4, 5, 11, 12], [4, 5, 9, 12], [5, 6, 7, 13], [5, 6, 8, 10], [5, 6, 12, 13], [1, 5, 7, 10], [2, 5, 7, 9], [3, 5, 8, 13], [5, 7, 8, 9], [5, 8, 9, 13], [5, 9, 10, 11], [2, 5, 12, 13], [1, 6, 7, 12], [1, 6, 8, 11], [1, 6, 9, 10], [2, 6, 7, 13], [2, 6, 8, 12], [2, 6, 9, 11], [2, 6, 10, 11], [3, 6, 7, 10], [3, 6, 8, 13], [3, 6, 10, 11], [4, 6, 8, 12], [4, 6, 9, 13], [4, 6, 9, 10], [5, 6, 10, 12], [5, 6, 10, 13], [5, 6, 11, 12], [6, 7, 8, 10], [6, 7, 12, 13], [4, 6, 8, 9], [6, 8, 11, 13], [3, 6, 9, 10], [5, 6, 9, 10], [2, 6, 10, 13], [1, 7, 8, 10], [2, 7, 8, 11], [2, 7, 9, 10], [3, 7, 8, 12], [3, 7, 9, 11], [4, 7, 8, 10], [4, 7, 8, 13], [4, 7, 9, 12], [4, 7, 10, 11], [5, 7, 9, 13], [5, 7, 10, 12], [6, 7, 10, 13], [6, 7, 11, 12], [6, 7, 10, 12], [2, 7, 8, 13], [7, 8, 12, 13], [3, 7, 9, 13], [2, 7, 10, 12], [2, 7, 11, 12], [3, 7, 12, 13], [2, 8, 9, 11], [2, 8, 9, 12], [3, 8, 9, 10], [4, 8, 9, 11], [4, 8, 9, 13], [4, 8, 10, 12], [5, 8, 9, 12], [5, 8, 10, 11], [6, 8, 9, 12], [6, 8, 9, 13], [6, 8, 10, 12], [7, 8, 10, 13], [7, 8, 11, 12], [3, 8, 9, 13], [8, 9, 12, 13], [4, 8, 12, 13], [3, 9, 10, 11], [6, 9, 10, 11], [7, 9, 10, 12], [8, 9, 10, 13], [8, 9, 11, 12], [5, 9, 10, 13], [8, 9, 10, 12], [9, 10, 12, 13], [3, 9, 11, 12], [6, 9, 12, 13], [9, 10, 11, 12], [4, 10, 11, 12], [10, 11, 12, 13], [4, 9, 11, 12]]
questions = []
temp = [] #用于去重的临时列表
if len(questions)==0:
nums = [i for i in range(1, number)]
for num_seq in itertools.permutations(nums, 4):
for ops_seq in itertools.product(['+', '-', '*', '/'], repeat=3):
if eval(f'{num_seq[0]} {ops_seq[0]} {num_seq[1]} {ops_seq[1]} {num_seq[2]} {ops_seq[2]} {num_seq[3]}') == 24:
questions.append([num_seq[0], num_seq[1], num_seq[2], num_seq[3]])
for i in questions:
i.sort() #列表中的列表排序后去重
if i not in temp:
temp.append(i)
questions = temp
# print(questions)
question = random.sample(questions, 1)[0]
for num_seq in itertools.permutations(question):
for ops_seq in itertools.product(['+', '-', '*', '/'], repeat=3):
# 对于每个组合,计算表达式的值,并判断是否等于24
if eval( f'{num_seq[0]} {ops_seq[0]} {num_seq[1]} {ops_seq[1]} {num_seq[2]} {ops_seq[2]} {num_seq[3]}') == 24:
print( f"{num_seq[0]} {ops_seq[0]} {num_seq[1]} {ops_seq[1]} {num_seq[2]} {ops_seq[2]} {num_seq[3]}")
print('\n' * 20)
print(question )
detailAnswer(10)