Custom permutation, Equal distribution of equal size groups

老子叫甜甜 提交于 2019-12-24 18:53:03

问题


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.

来源:https://stackoverflow.com/questions/54369353/custom-permutation-equal-distribution-of-equal-size-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!