I need to create all possible sequences of three arithmetic operators (+, -, *, /).
For list operators = ['+', '-', '*', '/']
, i have tried using list(itertools.combinations_with_replacement(operators ,3))
, which returns this list:
[('+', '+', '+'), ('+', '+', '-'), ('+', '+', '/'), ('+', '+', '*'), ('+', '-', '-'), ('+', '-', '/'), ('+', '-', '*'), ('+', '/', '/'), ('+', '/', '*'), ('+', '*', '*'), ('-', '-', '-'), ('-', '-', '/'), ('-', '-', '*'), ('-', '/', '/'), ('-', '/', '*'), ('-', '*', '*'), ('/', '/', '/'), ('/', '/', '*'), ('/', '*', '*'), ('*', '*', '*')]
The problem is that I also need combinations like ('*', '+', '*')
, which are not included. I have also tried itertools.permutations(operators, 3)
, but in this case, operators won't be repeated.
How can I get all the desired results?