Custom permutation, Equal distribution of equal si

2019-08-27 03:40发布

问题:

I made a post a few months ago here Custom permutation, Equal distribution of pairs. I wanted to generate pairs unique to each other while never containing the same pair.

I got an excellent answer from Thierry Lathuille on here with this code.

def pairs(n):
    for diff in range(1, n):
        starts_seen = set()
        index = 0
        for i in range(n):
            pair = [index]
            starts_seen.add(index)
            index = (index+diff) % n
            pair.append(index)
            yield pair
            index = (index+diff) % n
            if index in starts_seen:
                index = (index+1) % n

pairs = list(pair for pair in pairs(n))
print(pairs)

Now rather than create pairs I want to make the groups in to 3s or 4s etc.. I'm struggling once again on how to go about this. I'm guessing my existing code can be manipulated to accomplish this but I can't seem to get anything even worth sharing.. I can create groups of three but some of them contain 2 or 3 of the same index ex: (1,2,1), (2,2,2). which is not desirable.