我们有以下python列表:[1,2,3,10]
我要完成以下操作:创建一个接受列表并从算术运算列表中找出数字的函数:['+', '-', '/','*']
哪些组合给我们6作为答案。我们不想重复,所以我们不想在解决方案中使用2*3
和3*2
。我们确实要列出未使用的数字,所以是(这里是1和10)。 2/1*3=6.0
,2*3/1=6.0
,3/1*2=6.0
,3*2/1=6.0
都被认为是等效的,因为我们使用相同的数字而不管操作是什么,也没有使用10。我希望函数成为足够通用,因此我可以从那里使用它来表示1到9之间的数字。
感谢您的帮助。我尝试使用itertools和permutation来获取所有可能组合的列表,但这似乎不必要,并且产生了2/1*3=6.0
,2*3/1=6.0
,3/1*2=6.0
,3*2/1=6.0
包含在内的问题。列表,很难过滤掉。
使用itertools的示例:
from itertools import chain, permutations
def powerset(iterable):
xs = list(iterable)
return chain.from_iterable(permutations(xs,n) for n in range(len(xs)+1) )
lst_expr = []
for operands in map(list, powerset(['1','2','3','10'])):
n = len(operands)
#print operands
if n > 1:
all_operators = map(list, permutations(['+','-','*','/'],n-1))
#print all_operators, operands
for operators in all_operators:
exp = operands[0]
i = 1
for operator in operators:
exp += operator + operands[i]
i += 1
lst_expr += [exp]
lst_stages=[]
for equation in lst_expr:
if eval(equation) == 6:
lst_stages.append(equation)
eq = str(equation) + '=' + str(eval(equation))
print(eq)
答案 0 :(得分:2)
我喜欢sanyash的解决方案,但我认为通过使用操作数的组合而不是进行置换,可以更优雅地完成,直到找到具有正确值的第一个为止:
from itertools import chain, permutations, combinations
def powerset(iterable):
xs = list(iterable)
return chain.from_iterable(combinations(xs, n) for n in range(len(xs) + 1))
def get_first_perm_with_value(operands, operators, expected_value):
if len(operands) == 0 or len(operands) == 0:
return []
all_operators = list(map(list, permutations(operators, len(operands) - 1)))
all_operands = list(map(list, permutations(operands, len(operands))))
for operator in all_operators:
for operand in all_operands:
result = [""] * (len(operand) + len(operator))
result[::2] = operand
result[1::2] = operator
eq = ''.join(result)
if int(eval(eq)) == expected_value:
return [(f'{eq}={expected_value}', operands)]
return []
lst_expr = []
for operands in map(list, powerset(['1', '2', '3', '10'])):
lst_expr += get_first_perm_with_value(operands, ['+','-','*','/'], 6)
print(lst_expr)
返回:
[('2*3=6', ['2', '3']), ('2*3/1=6', ['1', '2', '3']), ('1+10/2=6', ['1', '2', '10']), ('2*10/3=6', ['2', '3', '10']), ('2*3+1/10=6', ['1', '2', '3', '10'])]
答案 1 :(得分:1)
这是可能的解决方案。我们需要保留已用数字的排序元组,以避免重复出现2 * 3和3 * 2。
from itertools import chain, permutations
def powerset(iterable):
xs = list(iterable)
return chain.from_iterable(permutations(xs,n) for n in range(len(xs)+1) )
lst_expr = []
for operands in map(list, powerset(['1','2','3','10'])):
n = len(operands)
#print operands
if n > 1:
all_operators = map(list, permutations(['+','-','*','/'],n-1))
#print all_operators, operands
for operators in all_operators:
exp = operands[0]
numbers = (operands[0],)
i = 1
for operator in operators:
exp += operator + operands[i]
numbers += (operands[i],)
i += 1
lst_expr += [{'exp': exp, 'numbers': tuple(sorted(numbers))}]
lst_stages=[]
numbers_sets = set()
for item in lst_expr:
equation = item['exp']
numbers = item['numbers']
if numbers not in numbers_sets and eval(equation) == 6:
lst_stages.append(equation)
eq = str(equation) + '=' + str(eval(equation))
print(eq, numbers)
numbers_sets.add(numbers)
输出:
2*3=6 ('2', '3')
1+10/2=6.0 ('1', '10', '2')
2/1*3=6.0 ('1', '2', '3')