开发者社区> 问答> 正文

如何对列表的所有元素执行数学运算?

所以基本上我有一个列表[[40,1,3,4,20],如果有一个排列,我可以返回TRUE,我可以重新排列列表中的数字并结合数学运算,得出总计42。这些运算符为:(+,-,*。示例为20 * 4-40 + 3-1 = 42,因此它将为列表[40,1,3,4,20]返回true`。 。

对于这个问题,我尝试使用itertool的替换功能组合来获取所有可能的运算符组合的列表:

from itertools import permutations, combinations, combinations_with_replacement
ops = []
perm = permutations([40,1,3,4,20], 5)
comb = combinations_with_replacement(["+","-","\*], 5)
for i in list(comb):
    ops.append(i)

print(ops)

这给了我:

[('+', '+', '+', '+', '+'),
 ('+', '+', '+', '+', '-'),
 ('+', '+', '+', '+', '\*),
 ('+', '+', '+', '-', '-'),
 ('+', '+', '+', '-', '\*),
 ('+', '+', '+', '\*, '\*),
 ('+', '+', '-', '-', '-'),
 ('+', '+', '-', '-', '\*),
 ('+', '+', '-', '\*, '\*),
 ('+', '+', '\*, '\*, '\*),
 ('+', '-', '-', '-', '-'),
 ('+', '-', '-', '-', '\*),
 ('+', '-', '-', '\*, '\*),
 ('+', '-', '\*, '\*, '\*),
 ('+', '\*, '\*, '\*, '\*),
 ('-', '-', '-', '-', '-'),
 ('-', '-', '-', '-', '\*),
 ('-', '-', '-', '\*, '\*),
 ('-', '-', '\*, '\*, '\*),
 ('-', '\*, '\*, '\*, '\*),
 ('\*, '\*, '\*, '\*, '\*)]

我将如何应用这21种独特的数学运算组合并将其迭代到列表中的元素上?我尝试了几件事,但一切都变得有些毛茸茸和令人困惑。

问题来源:stackoverflow

展开
收起
is大龙 2020-03-23 16:18:54 593 0
1 条回答
写回答
取消 提交回答
    • To avoid looking for the operator from symbol, I'd suggest to directly use the operators
    • then the operator sublist, should be one element smaller than the value sublists, 5 values need 4 operators
    • to get all the possibilities, use ` product ` for the operators

    对于值的每个子列表,对于运算符的每个子列表:计算结果

    • apply the operator on the previous value and the current one
    • you can now check if is equals your goal value

    • it it matches, some formatting, and you're done with an expression

      from itertools import permutations, product, chain, zip_longest from operator import add, sub, mul

      def operator_to_symbol(ope): return {add: "+", sub: "-", mul: "*}.get(ope, "")

      def format_result(values, ops): return " ".join(list(chain(*ip_longest(values, ops)))[:-1])

      def evaluate(values, operators): v = values[0] for idx, val in enumerate(values[1:]): v = operators[idx](v, val) return v

      if name == "main": perm_values = list(permutations([40, 1, 3, 4, 20], 5)) comb_operator = list(product([add, sub, mul], repeat=4))

      goal = 42
      for p in perm_values:
          for c in comb_operator:
              v = evaluate(p, c)
              if v == 42:
                  print(format_result(map(str, p), list(map(operator_to_symbol, c))), "=", goal)
      

    仅给出一个独特的结果:

    4 * 20 - 40 - 1 + 3 = 42
    4 * 20 - 40 + 3 - 1 = 42
    4 * 20 - 1 - 40 + 3 = 42
    4 * 20 - 1 + 3 - 40 = 42
    4 * 20 + 3 - 40 - 1 = 42
    4 * 20 + 3 - 1 - 40 = 42
    20 * 4 - 40 - 1 + 3 = 42
    20 * 4 - 40 + 3 - 1 = 42
    20 * 4 - 1 - 40 + 3 = 42
    20 * 4 - 1 + 3 - 40 = 42
    20 * 4 + 3 - 40 - 1 = 42
    20 * 4 + 3 - 1 - 40 = 42
    

    回答来源:stackoverflow

    2020-03-23 16:18:59
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载