Search code examples
pythonnested-loopspostfix-notation

Python - Generate Random Operations for Postfix Notation List


I'm attempting to produce a list that has four numbers (from 1 to 9) and three operations, represented using postfix notation. I'm trying to include the four main operations: +, -, *, and /.

There are 5 possible forms for this amount of numbers and operations, and they are as follows. An "O" represents a number and an "X" represents an operation:

OOXOOXX
OOOOXXX
OOOXOXX
OOXOXOX
OOOXXOX

I know that you have to use three nested loops. Given a list of four numbers called "s", I was trying something along the lines of this:

s = [1, 2, 3, 4]
ops = ['+', '-', '*', '/']

for i in ops:
    #Add a random operation from ops to one of the locations
    for i in ops:
        #Add a random operation from ops to one of the other locations
        for i in ops:
            s.append(i)
            #All of the instances have an operation in the last but I'm not sure how to 
            #select a random operation

Basically I'm stuck trying to select a random operation and place it in one of the designated locations. I'd also like to try and have every possible version of operations. Any help would be appreciated.

EDIT: Figured out how to get a list of all the possible operations. Still not sure how to place them in the list "s" though. I used the following:

allOps = list(itertools.product(ops, repeat=3))

Solution

  • Here's a program that generates all the possible combinations of 3 out of 4 operators and slots them into your 5 possible RPN forms with the 4 numbers from s in order.

    from itertools import product
    import operator
    
    all_ops = {
        '+': operator.__add__,
        '-': operator.__sub__,
        '*': operator.__mul__,
        '/': operator.__truediv__,
    }
    
    def rpn(cmd):
        ''' Evaluate a list of operations in Reverse Polish Notation '''
        stack = []
        for u in cmd:
            if u in all_ops:
                op = all_ops[u]
                y = stack.pop()
                x = stack.pop()
                stack.append(op(x, y))
            else:
                stack.append(u)
        # If command is well-formed there should be only 1 item left on the stack
        return stack[-1]
    
    # Put all the different RPN expression patterns into a list of strings
    # O: number, X: operator
    forms = '''\
    OOXOOXX
    OOOOXXX
    OOOXOXX
    OOXOXOX
    OOOXXOX
    '''.splitlines()
    
    s = [1, 2, 3, 4]
    
    # Make all the combinations of length 3 of all the operators
    for ops in product(all_ops.keys(), repeat=3):
        print(ops)
        for form in forms:
            # Make an iterator for this combination of operators
            opit = iter(ops)
            # Make an iterator for the numbers in s
            numit = iter(s)
            # Slot the operators and numbers into the pattern
            cmd = [next(opit) if c == 'X' else next(numit) for c in form]
            # Evaluate the command
            print(cmd, rpn(cmd))
    

    output

    ('+', '+', '+')
    [1, 2, '+', 3, 4, '+', '+'] 10
    [1, 2, 3, 4, '+', '+', '+'] 10
    [1, 2, 3, '+', 4, '+', '+'] 10
    [1, 2, '+', 3, '+', 4, '+'] 10
    [1, 2, 3, '+', '+', 4, '+'] 10
    ('+', '+', '-')
    [1, 2, '+', 3, 4, '+', '-'] -4
    [1, 2, 3, 4, '+', '+', '-'] -8
    [1, 2, 3, '+', 4, '+', '-'] -8
    [1, 2, '+', 3, '+', 4, '-'] 2
    [1, 2, 3, '+', '+', 4, '-'] 2
    ('+', '+', '*')
    [1, 2, '+', 3, 4, '+', '*'] 21
    [1, 2, 3, 4, '+', '+', '*'] 9
    [1, 2, 3, '+', 4, '+', '*'] 9
    [1, 2, '+', 3, '+', 4, '*'] 24
    [1, 2, 3, '+', '+', 4, '*'] 24
    ('+', '+', '/')
    [1, 2, '+', 3, 4, '+', '/'] 0.42857142857142855
    [1, 2, 3, 4, '+', '+', '/'] 0.1111111111111111
    [1, 2, 3, '+', 4, '+', '/'] 0.1111111111111111
    [1, 2, '+', 3, '+', 4, '/'] 1.5
    [1, 2, 3, '+', '+', 4, '/'] 1.5
    ('+', '-', '+')
    [1, 2, '+', 3, 4, '-', '+'] 2
    [1, 2, 3, 4, '+', '-', '+'] -4
    [1, 2, 3, '+', 4, '-', '+'] 2
    [1, 2, '+', 3, '-', 4, '+'] 4
    [1, 2, 3, '+', '-', 4, '+'] 0
    ('+', '-', '-')
    [1, 2, '+', 3, 4, '-', '-'] 4
    [1, 2, 3, 4, '+', '-', '-'] 6
    [1, 2, 3, '+', 4, '-', '-'] 0
    [1, 2, '+', 3, '-', 4, '-'] -4
    [1, 2, 3, '+', '-', 4, '-'] -8
    ('+', '-', '*')
    [1, 2, '+', 3, 4, '-', '*'] -3
    [1, 2, 3, 4, '+', '-', '*'] -5
    [1, 2, 3, '+', 4, '-', '*'] 1
    [1, 2, '+', 3, '-', 4, '*'] 0
    [1, 2, 3, '+', '-', 4, '*'] -16
    ('+', '-', '/')
    [1, 2, '+', 3, 4, '-', '/'] -3.0
    [1, 2, 3, 4, '+', '-', '/'] -0.2
    [1, 2, 3, '+', 4, '-', '/'] 1.0
    [1, 2, '+', 3, '-', 4, '/'] 0.0
    [1, 2, 3, '+', '-', 4, '/'] -1.0
    ('+', '*', '+')
    [1, 2, '+', 3, 4, '*', '+'] 15
    [1, 2, 3, 4, '+', '*', '+'] 15
    [1, 2, 3, '+', 4, '*', '+'] 21
    [1, 2, '+', 3, '*', 4, '+'] 13
    [1, 2, 3, '+', '*', 4, '+'] 9
    ('+', '*', '-')
    [1, 2, '+', 3, 4, '*', '-'] -9
    [1, 2, 3, 4, '+', '*', '-'] -13
    [1, 2, 3, '+', 4, '*', '-'] -19
    [1, 2, '+', 3, '*', 4, '-'] 5
    [1, 2, 3, '+', '*', 4, '-'] 1
    ('+', '*', '*')
    [1, 2, '+', 3, 4, '*', '*'] 36
    [1, 2, 3, 4, '+', '*', '*'] 14
    [1, 2, 3, '+', 4, '*', '*'] 20
    [1, 2, '+', 3, '*', 4, '*'] 36
    [1, 2, 3, '+', '*', 4, '*'] 20
    ('+', '*', '/')
    [1, 2, '+', 3, 4, '*', '/'] 0.25
    [1, 2, 3, 4, '+', '*', '/'] 0.07142857142857142
    [1, 2, 3, '+', 4, '*', '/'] 0.05
    [1, 2, '+', 3, '*', 4, '/'] 2.25
    [1, 2, 3, '+', '*', 4, '/'] 1.25
    ('+', '/', '+')
    [1, 2, '+', 3, 4, '/', '+'] 3.75
    [1, 2, 3, 4, '+', '/', '+'] 1.2857142857142856
    [1, 2, 3, '+', 4, '/', '+'] 2.25
    [1, 2, '+', 3, '/', 4, '+'] 5.0
    [1, 2, 3, '+', '/', 4, '+'] 4.2
    ('+', '/', '-')
    [1, 2, '+', 3, 4, '/', '-'] 2.25
    [1, 2, 3, 4, '+', '/', '-'] 0.7142857142857143
    [1, 2, 3, '+', 4, '/', '-'] -0.25
    [1, 2, '+', 3, '/', 4, '-'] -3.0
    [1, 2, 3, '+', '/', 4, '-'] -3.8
    ('+', '/', '*')
    [1, 2, '+', 3, 4, '/', '*'] 2.25
    [1, 2, 3, 4, '+', '/', '*'] 0.2857142857142857
    [1, 2, 3, '+', 4, '/', '*'] 1.25
    [1, 2, '+', 3, '/', 4, '*'] 4.0
    [1, 2, 3, '+', '/', 4, '*'] 0.8
    ('+', '/', '/')
    [1, 2, '+', 3, 4, '/', '/'] 4.0
    [1, 2, 3, 4, '+', '/', '/'] 3.5
    [1, 2, 3, '+', 4, '/', '/'] 0.8
    [1, 2, '+', 3, '/', 4, '/'] 0.25
    [1, 2, 3, '+', '/', 4, '/'] 0.05
    ('-', '+', '+')
    [1, 2, '-', 3, 4, '+', '+'] 6
    [1, 2, 3, 4, '-', '+', '+'] 2
    [1, 2, 3, '-', 4, '+', '+'] 4
    [1, 2, '-', 3, '+', 4, '+'] 6
    [1, 2, 3, '-', '+', 4, '+'] 4
    ('-', '+', '-')
    [1, 2, '-', 3, 4, '+', '-'] -8
    [1, 2, 3, 4, '-', '+', '-'] 0
    [1, 2, 3, '-', 4, '+', '-'] -2
    [1, 2, '-', 3, '+', 4, '-'] -2
    [1, 2, 3, '-', '+', 4, '-'] -4
    ('-', '+', '*')
    [1, 2, '-', 3, 4, '+', '*'] -7
    [1, 2, 3, 4, '-', '+', '*'] 1
    [1, 2, 3, '-', 4, '+', '*'] 3
    [1, 2, '-', 3, '+', 4, '*'] 8
    [1, 2, 3, '-', '+', 4, '*'] 0
    ('-', '+', '/')
    [1, 2, '-', 3, 4, '+', '/'] -0.14285714285714285
    [1, 2, 3, 4, '-', '+', '/'] 1.0
    [1, 2, 3, '-', 4, '+', '/'] 0.3333333333333333
    [1, 2, '-', 3, '+', 4, '/'] 0.5
    [1, 2, 3, '-', '+', 4, '/'] 0.0
    ('-', '-', '+')
    [1, 2, '-', 3, 4, '-', '+'] -2
    [1, 2, 3, 4, '-', '-', '+'] 4
    [1, 2, 3, '-', 4, '-', '+'] -4
    [1, 2, '-', 3, '-', 4, '+'] 0
    [1, 2, 3, '-', '-', 4, '+'] 6
    ('-', '-', '-')
    [1, 2, '-', 3, 4, '-', '-'] 0
    [1, 2, 3, 4, '-', '-', '-'] -2
    [1, 2, 3, '-', 4, '-', '-'] 6
    [1, 2, '-', 3, '-', 4, '-'] -8
    [1, 2, 3, '-', '-', 4, '-'] -2
    ('-', '-', '*')
    [1, 2, '-', 3, 4, '-', '*'] 1
    [1, 2, 3, 4, '-', '-', '*'] 3
    [1, 2, 3, '-', 4, '-', '*'] -5
    [1, 2, '-', 3, '-', 4, '*'] -16
    [1, 2, 3, '-', '-', 4, '*'] 8
    ('-', '-', '/')
    [1, 2, '-', 3, 4, '-', '/'] 1.0
    [1, 2, 3, 4, '-', '-', '/'] 0.3333333333333333
    [1, 2, 3, '-', 4, '-', '/'] -0.2
    [1, 2, '-', 3, '-', 4, '/'] -1.0
    [1, 2, 3, '-', '-', 4, '/'] 0.5
    ('-', '*', '+')
    [1, 2, '-', 3, 4, '*', '+'] 11
    [1, 2, 3, 4, '-', '*', '+'] -1
    [1, 2, 3, '-', 4, '*', '+'] -3
    [1, 2, '-', 3, '*', 4, '+'] 1
    [1, 2, 3, '-', '*', 4, '+'] 3
    ('-', '*', '-')
    [1, 2, '-', 3, 4, '*', '-'] -13
    [1, 2, 3, 4, '-', '*', '-'] 3
    [1, 2, 3, '-', 4, '*', '-'] 5
    [1, 2, '-', 3, '*', 4, '-'] -7
    [1, 2, 3, '-', '*', 4, '-'] -5
    ('-', '*', '*')
    [1, 2, '-', 3, 4, '*', '*'] -12
    [1, 2, 3, 4, '-', '*', '*'] -2
    [1, 2, 3, '-', 4, '*', '*'] -4
    [1, 2, '-', 3, '*', 4, '*'] -12
    [1, 2, 3, '-', '*', 4, '*'] -4
    ('-', '*', '/')
    [1, 2, '-', 3, 4, '*', '/'] -0.08333333333333333
    [1, 2, 3, 4, '-', '*', '/'] -0.5
    [1, 2, 3, '-', 4, '*', '/'] -0.25
    [1, 2, '-', 3, '*', 4, '/'] -0.75
    [1, 2, 3, '-', '*', 4, '/'] -0.25
    ('-', '/', '+')
    [1, 2, '-', 3, 4, '/', '+'] -0.25
    [1, 2, 3, 4, '-', '/', '+'] -1.0
    [1, 2, 3, '-', 4, '/', '+'] 0.75
    [1, 2, '-', 3, '/', 4, '+'] 3.6666666666666665
    [1, 2, 3, '-', '/', 4, '+'] 3.0
    ('-', '/', '-')
    [1, 2, '-', 3, 4, '/', '-'] -1.75
    [1, 2, 3, 4, '-', '/', '-'] 3.0
    [1, 2, 3, '-', 4, '/', '-'] 1.25
    [1, 2, '-', 3, '/', 4, '-'] -4.333333333333333
    [1, 2, 3, '-', '/', 4, '-'] -5.0
    ('-', '/', '*')
    [1, 2, '-', 3, 4, '/', '*'] -0.75
    [1, 2, 3, 4, '-', '/', '*'] -2.0
    [1, 2, 3, '-', 4, '/', '*'] -0.25
    [1, 2, '-', 3, '/', 4, '*'] -1.3333333333333333
    [1, 2, 3, '-', '/', 4, '*'] -4.0
    ('-', '/', '/')
    [1, 2, '-', 3, 4, '/', '/'] -1.3333333333333333
    [1, 2, 3, 4, '-', '/', '/'] -0.5
    [1, 2, 3, '-', 4, '/', '/'] -4.0
    [1, 2, '-', 3, '/', 4, '/'] -0.08333333333333333
    [1, 2, 3, '-', '/', 4, '/'] -0.25
    ('*', '+', '+')
    [1, 2, '*', 3, 4, '+', '+'] 9
    [1, 2, 3, 4, '*', '+', '+'] 15
    [1, 2, 3, '*', 4, '+', '+'] 11
    [1, 2, '*', 3, '+', 4, '+'] 9
    [1, 2, 3, '*', '+', 4, '+'] 11
    ('*', '+', '-')
    [1, 2, '*', 3, 4, '+', '-'] -5
    [1, 2, 3, 4, '*', '+', '-'] -13
    [1, 2, 3, '*', 4, '+', '-'] -9
    [1, 2, '*', 3, '+', 4, '-'] 1
    [1, 2, 3, '*', '+', 4, '-'] 3
    ('*', '+', '*')
    [1, 2, '*', 3, 4, '+', '*'] 14
    [1, 2, 3, 4, '*', '+', '*'] 14
    [1, 2, 3, '*', 4, '+', '*'] 10
    [1, 2, '*', 3, '+', 4, '*'] 20
    [1, 2, 3, '*', '+', 4, '*'] 28
    ('*', '+', '/')
    [1, 2, '*', 3, 4, '+', '/'] 0.2857142857142857
    [1, 2, 3, 4, '*', '+', '/'] 0.07142857142857142
    [1, 2, 3, '*', 4, '+', '/'] 0.1
    [1, 2, '*', 3, '+', 4, '/'] 1.25
    [1, 2, 3, '*', '+', 4, '/'] 1.75
    ('*', '-', '+')
    [1, 2, '*', 3, 4, '-', '+'] 1
    [1, 2, 3, 4, '*', '-', '+'] -9
    [1, 2, 3, '*', 4, '-', '+'] 3
    [1, 2, '*', 3, '-', 4, '+'] 3
    [1, 2, 3, '*', '-', 4, '+'] -1
    ('*', '-', '-')
    [1, 2, '*', 3, 4, '-', '-'] 3
    [1, 2, 3, 4, '*', '-', '-'] 11
    [1, 2, 3, '*', 4, '-', '-'] -1
    [1, 2, '*', 3, '-', 4, '-'] -5
    [1, 2, 3, '*', '-', 4, '-'] -9
    ('*', '-', '*')
    [1, 2, '*', 3, 4, '-', '*'] -2
    [1, 2, 3, 4, '*', '-', '*'] -10
    [1, 2, 3, '*', 4, '-', '*'] 2
    [1, 2, '*', 3, '-', 4, '*'] -4
    [1, 2, 3, '*', '-', 4, '*'] -20
    ('*', '-', '/')
    [1, 2, '*', 3, 4, '-', '/'] -2.0
    [1, 2, 3, 4, '*', '-', '/'] -0.1
    [1, 2, 3, '*', 4, '-', '/'] 0.5
    [1, 2, '*', 3, '-', 4, '/'] -0.25
    [1, 2, 3, '*', '-', 4, '/'] -1.25
    ('*', '*', '+')
    [1, 2, '*', 3, 4, '*', '+'] 14
    [1, 2, 3, 4, '*', '*', '+'] 25
    [1, 2, 3, '*', 4, '*', '+'] 25
    [1, 2, '*', 3, '*', 4, '+'] 10
    [1, 2, 3, '*', '*', 4, '+'] 10
    ('*', '*', '-')
    [1, 2, '*', 3, 4, '*', '-'] -10
    [1, 2, 3, 4, '*', '*', '-'] -23
    [1, 2, 3, '*', 4, '*', '-'] -23
    [1, 2, '*', 3, '*', 4, '-'] 2
    [1, 2, 3, '*', '*', 4, '-'] 2
    ('*', '*', '*')
    [1, 2, '*', 3, 4, '*', '*'] 24
    [1, 2, 3, 4, '*', '*', '*'] 24
    [1, 2, 3, '*', 4, '*', '*'] 24
    [1, 2, '*', 3, '*', 4, '*'] 24
    [1, 2, 3, '*', '*', 4, '*'] 24
    ('*', '*', '/')
    [1, 2, '*', 3, 4, '*', '/'] 0.16666666666666666
    [1, 2, 3, 4, '*', '*', '/'] 0.041666666666666664
    [1, 2, 3, '*', 4, '*', '/'] 0.041666666666666664
    [1, 2, '*', 3, '*', 4, '/'] 1.5
    [1, 2, 3, '*', '*', 4, '/'] 1.5
    ('*', '/', '+')
    [1, 2, '*', 3, 4, '/', '+'] 2.75
    [1, 2, 3, 4, '*', '/', '+'] 1.1666666666666667
    [1, 2, 3, '*', 4, '/', '+'] 2.5
    [1, 2, '*', 3, '/', 4, '+'] 4.666666666666667
    [1, 2, 3, '*', '/', 4, '+'] 4.166666666666667
    ('*', '/', '-')
    [1, 2, '*', 3, 4, '/', '-'] 1.25
    [1, 2, 3, 4, '*', '/', '-'] 0.8333333333333334
    [1, 2, 3, '*', 4, '/', '-'] -0.5
    [1, 2, '*', 3, '/', 4, '-'] -3.3333333333333335
    [1, 2, 3, '*', '/', 4, '-'] -3.8333333333333335
    ('*', '/', '*')
    [1, 2, '*', 3, 4, '/', '*'] 1.5
    [1, 2, 3, 4, '*', '/', '*'] 0.16666666666666666
    [1, 2, 3, '*', 4, '/', '*'] 1.5
    [1, 2, '*', 3, '/', 4, '*'] 2.6666666666666665
    [1, 2, 3, '*', '/', 4, '*'] 0.6666666666666666
    ('*', '/', '/')
    [1, 2, '*', 3, 4, '/', '/'] 2.6666666666666665
    [1, 2, 3, 4, '*', '/', '/'] 6.0
    [1, 2, 3, '*', 4, '/', '/'] 0.6666666666666666
    [1, 2, '*', 3, '/', 4, '/'] 0.16666666666666666
    [1, 2, 3, '*', '/', 4, '/'] 0.041666666666666664
    ('/', '+', '+')
    [1, 2, '/', 3, 4, '+', '+'] 7.5
    [1, 2, 3, 4, '/', '+', '+'] 3.75
    [1, 2, 3, '/', 4, '+', '+'] 5.666666666666667
    [1, 2, '/', 3, '+', 4, '+'] 7.5
    [1, 2, 3, '/', '+', 4, '+'] 5.666666666666666
    ('/', '+', '-')
    [1, 2, '/', 3, 4, '+', '-'] -6.5
    [1, 2, 3, 4, '/', '+', '-'] -1.75
    [1, 2, 3, '/', 4, '+', '-'] -3.666666666666667
    [1, 2, '/', 3, '+', 4, '-'] -0.5
    [1, 2, 3, '/', '+', 4, '-'] -2.3333333333333335
    ('/', '+', '*')
    [1, 2, '/', 3, 4, '+', '*'] 3.5
    [1, 2, 3, 4, '/', '+', '*'] 2.75
    [1, 2, 3, '/', 4, '+', '*'] 4.666666666666667
    [1, 2, '/', 3, '+', 4, '*'] 14.0
    [1, 2, 3, '/', '+', 4, '*'] 6.666666666666666
    ('/', '+', '/')
    [1, 2, '/', 3, 4, '+', '/'] 0.07142857142857142
    [1, 2, 3, 4, '/', '+', '/'] 0.36363636363636365
    [1, 2, 3, '/', 4, '+', '/'] 0.21428571428571427
    [1, 2, '/', 3, '+', 4, '/'] 0.875
    [1, 2, 3, '/', '+', 4, '/'] 0.41666666666666663
    ('/', '-', '+')
    [1, 2, '/', 3, 4, '-', '+'] -0.5
    [1, 2, 3, 4, '/', '-', '+'] 2.25
    [1, 2, 3, '/', 4, '-', '+'] -2.3333333333333335
    [1, 2, '/', 3, '-', 4, '+'] 1.5
    [1, 2, 3, '/', '-', 4, '+'] 4.333333333333333
    ('/', '-', '-')
    [1, 2, '/', 3, 4, '-', '-'] 1.5
    [1, 2, 3, 4, '/', '-', '-'] -0.25
    [1, 2, 3, '/', 4, '-', '-'] 4.333333333333334
    [1, 2, '/', 3, '-', 4, '-'] -6.5
    [1, 2, 3, '/', '-', 4, '-'] -3.6666666666666665
    ('/', '-', '*')
    [1, 2, '/', 3, 4, '-', '*'] -0.5
    [1, 2, 3, 4, '/', '-', '*'] 1.25
    [1, 2, 3, '/', 4, '-', '*'] -3.3333333333333335
    [1, 2, '/', 3, '-', 4, '*'] -10.0
    [1, 2, 3, '/', '-', 4, '*'] 1.3333333333333335
    ('/', '-', '/')
    [1, 2, '/', 3, 4, '-', '/'] -0.5
    [1, 2, 3, 4, '/', '-', '/'] 0.8
    [1, 2, 3, '/', 4, '-', '/'] -0.3
    [1, 2, '/', 3, '-', 4, '/'] -0.625
    [1, 2, 3, '/', '-', 4, '/'] 0.08333333333333334
    ('/', '*', '+')
    [1, 2, '/', 3, 4, '*', '+'] 12.5
    [1, 2, 3, 4, '/', '*', '+'] 2.5
    [1, 2, 3, '/', 4, '*', '+'] 3.6666666666666665
    [1, 2, '/', 3, '*', 4, '+'] 5.5
    [1, 2, 3, '/', '*', 4, '+'] 4.666666666666667
    ('/', '*', '-')
    [1, 2, '/', 3, 4, '*', '-'] -11.5
    [1, 2, 3, 4, '/', '*', '-'] -0.5
    [1, 2, 3, '/', 4, '*', '-'] -1.6666666666666665
    [1, 2, '/', 3, '*', 4, '-'] -2.5
    [1, 2, 3, '/', '*', 4, '-'] -3.3333333333333335
    ('/', '*', '*')
    [1, 2, '/', 3, 4, '*', '*'] 6.0
    [1, 2, 3, 4, '/', '*', '*'] 1.5
    [1, 2, 3, '/', 4, '*', '*'] 2.6666666666666665
    [1, 2, '/', 3, '*', 4, '*'] 6.0
    [1, 2, 3, '/', '*', 4, '*'] 2.6666666666666665
    ('/', '*', '/')
    [1, 2, '/', 3, 4, '*', '/'] 0.041666666666666664
    [1, 2, 3, 4, '/', '*', '/'] 0.6666666666666666
    [1, 2, 3, '/', 4, '*', '/'] 0.375
    [1, 2, '/', 3, '*', 4, '/'] 0.375
    [1, 2, 3, '/', '*', 4, '/'] 0.16666666666666666
    ('/', '/', '+')
    [1, 2, '/', 3, 4, '/', '+'] 1.25
    [1, 2, 3, 4, '/', '/', '+'] 3.6666666666666665
    [1, 2, 3, '/', 4, '/', '+'] 1.1666666666666667
    [1, 2, '/', 3, '/', 4, '+'] 4.166666666666667
    [1, 2, 3, '/', '/', 4, '+'] 5.5
    ('/', '/', '-')
    [1, 2, '/', 3, 4, '/', '-'] -0.25
    [1, 2, 3, 4, '/', '/', '-'] -1.6666666666666665
    [1, 2, 3, '/', 4, '/', '-'] 0.8333333333333334
    [1, 2, '/', 3, '/', 4, '-'] -3.8333333333333335
    [1, 2, 3, '/', '/', 4, '-'] -2.5
    ('/', '/', '*')
    [1, 2, '/', 3, 4, '/', '*'] 0.375
    [1, 2, 3, 4, '/', '/', '*'] 2.6666666666666665
    [1, 2, 3, '/', 4, '/', '*'] 0.16666666666666666
    [1, 2, '/', 3, '/', 4, '*'] 0.6666666666666666
    [1, 2, 3, '/', '/', 4, '*'] 6.0
    ('/', '/', '/')
    [1, 2, '/', 3, 4, '/', '/'] 0.6666666666666666
    [1, 2, 3, 4, '/', '/', '/'] 0.375
    [1, 2, 3, '/', 4, '/', '/'] 6.0
    [1, 2, '/', 3, '/', 4, '/'] 0.041666666666666664
    [1, 2, 3, '/', '/', 4, '/'] 0.375
    

    Beware, this code does no error checking, so it will crash if some combination attempts to divide by zero.