Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Combinations vs Permutations

Not knowing the terminology to use, I am trying to achieve the following in python:

pairs = [perm for perm in permutations( range( 7 ), 2)]

This gives me the following:

pairs = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (2, 6), (3, 0), (3, 1), (3, 2), (3, 4), (3, 5), (3, 6), (4, 0), (4, 1), (4, 2), (4, 3), (4, 5), (4, 6), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 6), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5)]

Following Constraints: 1. Each pair only appears once in unique_groups 2. Pair are placed in groups such that the numbers (0 - 6) only appear one in the whole group of pairs

    Example [[(1, 2), (3, 4), (5, 6)], [(2, 1), (4, 3), (6, 5)] = Correct
    Example [(1, 2), (3, 4), (5, 1)], [(2, 1), (3, 4), (6, 5)]] = Incorrect 1 appears twice in the entry and pair (3, 4) is used twice in the set.


    unique_groups = do_something_cool(pairs)

    unique_groups = [
    [(0, 1), (2, 3), (4, 5)],
    [(0, 2), (1, 3), (4, 6)],
    [(0, 3), (1, 2), (5, 4)],
    [(0, 4), (1, 5), (2, 6)],
    [(0, 5), (1, 4), (2, 5)],
    [(0, 6), (2, 1), (3, 4)],
    [(1, 0), (2, 4), (3, 5)],
    [(1, 6), (2, 0), (4, 3)],
    [(3, 0), (4, 1), (5, 2)],
    [(3, 1), (4, 0), (5, 3)],
    [(3, 2), (5, 0), (6, 1)],
    [(3, 6), (4, 2), (5, 1)],
    [(5, 6)],
    [(6, 0)],
    [(6, 2)],
    [(6, 3)],
    [(6, 4)],
    [(6, 5)],
    ]

There might be multiple answers to this question. I am looking for something in the either the python libraries that does this or some simple code that does it.

Thanks for any help.

Update: The following code produces the right output:

pairs = [perm for perm in permutations( range(7), 2)]

list_o = []

while len(pairs) > 0:
   pair = pairs.pop(0)
   inserted = False
   for item in list_o:
      found = False
      for item2 in item:
         if pair[0] in item2 or pair[1] in item2:
            found = True
            break 
      if not found:
         item.append(pair)
         inserted = True
         break
   if not inserted:
      list_o.append([pair])

print(list_o)


[
[(0, 1), (2, 3), (4, 5)], 
[(0, 2), (1, 3), (4, 6)], 
[(0, 3), (1, 2), (5, 4)], 
[(0, 4), (1, 5), (2, 6)], 
[(0, 5), (1, 4), (3, 2)], 
[(0, 6), (2, 1), (3, 4)], 
[(1, 0), (2, 4), (3, 5)], 
[(1, 6), (2, 0), (4, 3)], 
[(2, 5), (3, 0), (4, 1)], 
[(3, 1), (4, 0), (5, 2)], 
[(3, 6), (4, 2), (5, 0)], 
[(5, 1), (6, 0)], 
[(5, 3), (6, 1)], 
[(5, 6)], [(6, 2)], 
[(6, 3)], [(6, 4)], 
[(6, 5)]
]

P.S. I am horrible at python is there a better way to do this? Also is there a way to have all entries be 3 pairs and not the double or single pair at the bottom?

Comments