2

I have a list with the following elements: A,B,C,D,E,F,G.
They are either suppose to true or false hence represented by 1 and 0 respectively.

I am supposed to get a combinations but the following restrictions stay:

  1. Element C and Fare to be true in all cases, ie,1`.
  2. When element A is true, element E, and G can be false.
  3. When element B is true, element D can be false.
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
zcfaynh
  • 31
  • 4
  • 3
    _"when element A is true, element E and G can be false"_ does it mean _"E and G can be false or true"_ or _"E and G have to be false"_ ? – furas Nov 28 '15 at 16:31
  • Looks to me like another *can you make my homework* question. I might be wrong (hope so), in which case, can you put what you have tried so far? We can help you from there. – Imanol Luengo Nov 28 '15 at 17:17
  • Sounds like homework. For your own sake, please read [this letter](http://meta.programmers.stackexchange.com/questions/6166). – Tom Zych Dec 02 '15 at 00:37

1 Answers1

1

What you want is not permutations, but product. Also, I interpret restrictions as:

  1. C and F cannot be false
  2. If A is false, E and G cannot be false
  3. If B is false, D cannot be false

With that, the code is as followed:

import pprint
from itertools import product

def myproduct():
    keys = 'abcdefg'
    values = [(0, 1) for k in keys]

    for value in product(*values):
        d = dict(zip(keys, value))
        # Skip: C and F that are 0 (False)
        if d['c'] == 0 or d['f'] == 0:
            continue

        # Skip: When A is false, E and G cannot be false
        if d['a'] == 0 and (d['e'] == 0 or d['g'] == 0):
            continue

        # Skip: When B is false, D cannot be false
        if d['b'] == 0 and d['d'] == 0:
            continue

        yield d  # This 'permutation' is good

for d in myproduct():
    pprint.pprint(d)

Output:

{'a': 0, 'b': 0, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1}
{'a': 0, 'b': 1, 'c': 1, 'd': 0, 'e': 1, 'f': 1, 'g': 1}
{'a': 0, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1}
{'a': 1, 'b': 0, 'c': 1, 'd': 1, 'e': 0, 'f': 1, 'g': 0}
{'a': 1, 'b': 0, 'c': 1, 'd': 1, 'e': 0, 'f': 1, 'g': 1}
{'a': 1, 'b': 0, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 0}
{'a': 1, 'b': 0, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1}
{'a': 1, 'b': 1, 'c': 1, 'd': 0, 'e': 0, 'f': 1, 'g': 0}
{'a': 1, 'b': 1, 'c': 1, 'd': 0, 'e': 0, 'f': 1, 'g': 1}
{'a': 1, 'b': 1, 'c': 1, 'd': 0, 'e': 1, 'f': 1, 'g': 0}
{'a': 1, 'b': 1, 'c': 1, 'd': 0, 'e': 1, 'f': 1, 'g': 1}
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 0, 'f': 1, 'g': 0}
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 0, 'f': 1, 'g': 1}
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 0}
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1}

Notes:

  • values is a list of (0, 1):

    [(0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1)]
    
  • Each value is a tuple of 7 numbers such as:

    (1, 1, 1, 0, 0, 1, 0)
    
  • d is a dictionary in which the keys are a, b, ... and the values are 0 and 1

Hai Vu
  • 37,849
  • 11
  • 66
  • 93