Countdown in Python using permutations and lambda
Question: I am trying to make a program that solves the following with `permutations`
and `lambda`:

You pick 5 numbers and a random number is generated, the aim is to use those 5
numbers to reach the target number. You are allowed to use each number once
with as many operators as you want (`+-*/`). I want the program to `print()`
all of the solutions

this is the code i have created so far

    
    
    from itertools import permutations
    import operator
    
    num1=3
    num2=5
    num3=7
    num4=2
    num5=10
    
    class Infix:
        def __init__(self, function):
            self.function = function
        def __ror__(self, other):
            return Infix(lambda x, self=self, other=other: self.function(other, x))
        def __or__(self, other):
            return self.function(other)
        def __rlshift__(self, other):
            return Infix(lambda x, self=self, other=other: self.function(other, x))
        def __rshift__(self, other):
            return self.function(other)
        def __call__(self, value1, value2):
            return self.function(value1, value2)
    
    """what the class infix does is it allows me to substitute operators such as +-*/ to functions as follows:"""
    
    multiply=Infix(lambda x,y: x*y) #this is a way of telling it that whenever i call x multiply y is the same as x*y 
    add=Infix(lambda x,y: x+y) #same here just for adding
    minus=Infix(lambda x,y: x-y)
    divide=Infix(lambda x,y: x/y)
    """the way infix can be called is if i say 3 |add| 4, which allows me to substitute 
    variables as the operators"""
    target = 50
    
    for w,x,y,z in permutations((|multiply|,|add|,|minus|,|divide|), 4): #telling it to substitute operators to the variables
        for a,b,c,d,e in permutations(num1,num2,num3,num4,num5), 5): 
            if a(w)b(x)c(y)d(z)e == target: #works out the sum
                print(a(w)b(x)c(y)d(z)e) #prints it if it equals my target
    

The only error in this code is the `for` loops because i don't know how to
substitute both operators and numbers at the same time, all the other parts of
the code are absolutely fine.

The expected output is all answers that work e.g. `3*2+5-7=4` if `target==4`.

What i asked this to do is run through all of the numbers with all of the
operators again and again to find answers that match the target, and then
print them out, but i am having difficulties trying to substitute the number
together with the operator.

I thought that `a(X)b(y)...` would work because if you substitute it, it is
`a(|add|)b(|multiply|)d...` and so on, but it turns out that it doesn't.

None of these work but i am trying to find a solution that does. It needs to
loop through the numbers and the operators at the same time and i don't know
how to do it!

## Error when running:

![this is the error i get when running](http://i.stack.imgur.com/xlubz.png)

## `Infix` working correctly:

![infix working proof](http://i.stack.imgur.com/BXrpL.png)


Answer: Finally, I think we get to the bottom of it. You are expecting the "bitwise
OR" operators `|` to somehow modify the `Infix` objects, so that when you do

    
    
    permutations((|multiply|,|add|,|minus|,|divide|), 4)
    

they "stay with" the objects. That can't happen; for example, you wouldn't do:

    
    
    x, y = 1, 2
    a = x+
    print(ay)
    

and expect `3`. Instead, you need to do:

    
    
    permutations((multiply, add, minus, divide), 4)
    

i.e. actually shuffle the `Infix` instances, then apply the operators later,
when calculating the comparison with `target`:

    
    
    if a |w| b |x| c |y| d |z| e == target:
    

There are a few other minor errors, but that should allow you to move
forwards.

