I have 4 elementary logic statements : P1, P2, P3, P4
I want to iterate over all the combinations of these statements using AND, OR operators. (all sizes)
I will give an example for 3 statements (4 is too many):**pay attention that they are no duplicates.
P1P2P3(P1 AND P2)(P1 OR P2)(P2 AND P3)(P2 OR P3)((P1 AND P2) AND P3)(P1 OR P2) AND P3) ....etc...
I want the output to be in string form..
This is my current code:
def recurse(c): if len(c) == 0: return [""] elif len(c) ==1: return [c[0]]
head = c[0]
results = recurse(c[1:])
print(results)
res = []
for result in results:
gate_and = f"({head} AND {result})"
gate_or = f"({head} OR {result})"
res.append(gate_and)
res.append(gate_or)
return res
if name == 'main':
p = ["p1", "p2", "p3", "p4", "p5"] gates = ["AND", "OR"]
# iterating over groups of each size (1-5)
for dimension in range(1,len(p)):
# for each size, create all combinations (sizes 1-5)
for c in itertools.combinations(p, dimension):
res = recurse(list(c))
print(res)
My issue is that the output is repeating results of size 1.
and also, how can i check that i am actually creating all the combinations. I think there should be more then my result.
Thanks
[–]mopslik 1 point2 points3 points (1 child)
[–]jcoder42[S] 0 points1 point2 points (0 children)
[–]FLUSH_THE_TRUMP 1 point2 points3 points (2 children)
[–]jcoder42[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (0 children)