0

4 one digit numbers and try to get all the possible combos 4511 like 4 + 5 + 1 x 1 Code to get first, 2nd 3rd and 4th numbers

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])
  • 1
    maybe helpful: https://stackoverflow.com/questions/39525993/trying-all-combinations-of-operations-on-list-of-variables/39528917 – chickity china chinese chicken Jul 25 '17 at 01:27
  • What are these "bunch of stuff" that you have tried? You are only showing code of how you did I/O. Even if it is inefficient, it helps to show others what you have tried so that they can give you useful advice. – Julian Chan Jul 25 '17 at 03:05
  • I actually wrote a blog post on how I solved whether this can be done for all numbers with a larger set of operators: https://medium.com/@matthewegan/the-sydney-train-carriage-problem-cf681c96668 – mattjegan Jul 25 '17 at 03:33

2 Answers2

1

An exhaustive search:

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)

Example output:

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

or

No solution for [7, 1, 0, 2]

Note: I like the recursive expression generator referred to in the comment above, but I just don't think recursively straight off.

Mike Robins
  • 1,733
  • 10
  • 14
  • 1
    I like your answer, but i don't know if OP would learn much from it, because it relies a lot on external libraries and he does seem like he is just starting out. – Julian Chan Jul 25 '17 at 03:22
  • 1
    Thanks @Julian I take your point but, I think that with the 'batteries included' nature of the Python distribution we should encourage everyone to learn the libraries. This question does look like an assignment problem. – Mike Robins Jul 25 '17 at 05:42
0

Use a recursive approach:

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))

Your I/O is wrong (first two lines), and I don't know if this is something that you have already tried.

Julian Chan
  • 446
  • 2
  • 8