Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 4个数字的所有+-/x组合_Python - Fatal编程技术网

Python 4个数字的所有+-/x组合

Python 4个数字的所有+-/x组合,python,Python,4个一位数的数字,并尝试获得所有可能的组合 4511 比如4+5+1 x 1 获取第一、第二、第三和第四个数字的代码 numbers = input("Input 4 numbers separated with , : ") numlist = numbers.split(",") print (numlist) No1 = int(numlist.pop(0)) No2 = int(numlist[0]) No3 = int(numlist[1]) No4 = int(numlist[2])

4个一位数的数字,并尝试获得所有可能的组合 4511 比如4+5+1 x 1 获取第一、第二、第三和第四个数字的代码

numbers = input("Input 4 numbers separated with , : ")
numlist = numbers.split(",")
print (numlist)
No1 = int(numlist.pop(0))
No2 = int(numlist[0])
No3 = int(numlist[1])
No4 = int(numlist[2])
详尽的搜索:

from random import randrange
from itertools import permutations

def solve(digits):
    for xs in permutations(digits):
        for ops in permutations('+-*/', 3):
            equation = reduce(lambda r, (x, op): '{0} {1} {2}'.format(r, op, float(x)), zip(xs[1:], ops), xs[0])
            try:
                if eval(equation) == 10:
                    yield equation.replace('.0','')
            except ZeroDivisionError:
                pass

digits = [randrange(0,10,1) for x in range(4)]
solutions = list(solve(digits))
if solutions:
    print '\n'.join(solutions)
else:
    print 'No solution for {0}'.format(digits)
示例输出:

2 * 5 / 1 + 0
2 * 5 / 1 - 0
2 * 5 + 0 / 1
2 * 5 - 0 / 1
2 / 1 * 5 + 0
2 / 1 * 5 - 0
5 * 2 / 1 + 0
5 * 2 / 1 - 0
5 * 2 + 0 / 1
5 * 2 - 0 / 1
5 / 1 * 2 + 0
5 / 1 * 2 - 0
0 + 2 * 5 / 1
0 + 2 / 1 * 5
0 + 5 * 2 / 1
0 + 5 / 1 * 2
0 / 1 + 2 * 5
0 / 1 + 5 * 2


注意:我喜欢上面评论中提到的递归表达式生成器,但我不直接递归地思考。

使用递归方法:

numbers = input("Input 4 numbers separated with , : ")
numlist = list(numbers)

def f(lst,carry,result):
    x = lst.pop(0)
    if lst == []:
        return carry+x == result or \
               carry-x == result or \
               carry*x == result or \
               carry/x == result
    elif carry==None:
        carry = x
        return f(lst,carry,result)
    else:
        return f(lst,carry+x,result) or\
               f(lst,carry-x,result) or\
               f(lst,carry*x,result) or\
               f(lst,carry/x,result)

print(f(numlist, None, 10))

您的I/O在前两行出错,我不知道这是否是您已经尝试过的东西。

可能有帮助:您尝试过的这些东西是什么?你只是在展示你如何进行I/O的代码。即使效率很低,也有助于向其他人展示你所做的尝试,以便他们能给你提供有用的建议。我实际上写了一篇博客文章,讨论了我如何解决是否可以用更大的运算符集对所有数字进行此操作:我喜欢你的答案,但我不知道OP是否能从中学到很多东西,因为它很大程度上依赖于外部库,他看起来才刚刚开始。谢谢@Julian我接受你的观点,但我认为,由于Python发行版的“电池包括在内”特性,我们应该鼓励每个人学习这些库。这个问题看起来确实像一个作业问题。
numbers = input("Input 4 numbers separated with , : ")
numlist = list(numbers)

def f(lst,carry,result):
    x = lst.pop(0)
    if lst == []:
        return carry+x == result or \
               carry-x == result or \
               carry*x == result or \
               carry/x == result
    elif carry==None:
        carry = x
        return f(lst,carry,result)
    else:
        return f(lst,carry+x,result) or\
               f(lst,carry-x,result) or\
               f(lst,carry*x,result) or\
               f(lst,carry/x,result)

print(f(numlist, None, 10))