Last active
December 13, 2023 13:56
-
-
Save guilt/1449d49473291f3be5886bd8a855931e to your computer and use it in GitHub Desktop.
Combinatorial Operator Interleaver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import product, permutations | |
""" | |
if __name__ == '__main__': | |
for expr in generateOperators([2,3,4], ['+','-']): | |
print(expr) | |
""" | |
def alternateBetween(items, operators): | |
lenItems = len(items) | |
lenOperators = len(operators) | |
itemIter = 0 | |
opIter = 0 | |
isItem = True | |
while True: | |
if isItem and itemIter < lenItems: | |
yield items[itemIter] | |
itemIter += 1 | |
elif not isItem and opIter < lenOperators: | |
yield operators[opIter] | |
opIter += 1 | |
else: | |
break | |
isItem = not isItem | |
def generateOperators(iterable, operators): | |
items = list(iterable) | |
lenItems = len(items) | |
for operatorMix in product(operators, repeat=lenItems-1): | |
yield list(alternateBetween(items, operatorMix)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment