16人の人間を4人で1グループ計4グループに分ける

detail.chiebukuro.yahoo.co.jp

import pprint
import functools

class gperm():

    def __init__(self):

        self.flag_list = [[0 for x in range(16)] for y in range(16)]

    def gsearch(self):
    
        for i in range(1, 6):

            add_list = []
            del_list = [j for j in range(16)]

            for k in range(4):

                d1 = del_list.pop(0)
                add_list.append(d1)
    
                a1 = list(self.get_off(d1) - set(add_list))
                a1.sort()

                d2 = a1.pop(0)
                del_list.remove(d2)
                add_list.append(d2)

                a2 = list(self.get_off(d1, d2) - set(add_list))
                a2.sort()

                d3 = a2.pop(0)
                del_list.remove(d3)
                add_list.append(d3)
                
                a3 = list(self.get_off(d1, d2, d3) - set(add_list))
                a3.sort()
 
                d4 = a3.pop(0)
                del_list.remove(d4)
                add_list.append(d4)

                print(d1,d2,d3,d4)
            
                self.flag_on([d1, d2, d3, d4], i)

            print('-' * 10)    

    def flag_on(self, data, n):

        for y in data:
            for x in data:
                if not self.flag_list[y][x]:
                    self.flag_list[y][x] = n

    def get_off(self, *v):

        data = [{x for x, y in enumerate(self.flag_list[i]) if not y} for i in v]

        ans = functools.reduce(lambda x, y: x & y, data)
        
        return ans

    def flag_print(self):

        pprint.pprint(self.flag_list)

        return(self.flag_list)

test = gperm()

test.gsearch()
test.flag_print()