Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9090453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:22:29+00:00 2026-06-16T22:22:29+00:00

I need to generate all possible pairings, but with the constraint that a particular

  • 0

I need to generate all possible pairings, but with the constraint that a particular pairing only occurs once in the results. So for example:

import itertools

for perm in itertools.permutations(range(9)):
    print zip(perm[::2], perm[1::2])

generates all possible two-paired permutations; here’s a small subset of the output:

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

How do I further filter it so that I only ever see (8,4) once (throughout all of the filtered permutations), and (8,5) only once, and (0,1) only once, and (4,7) only once, etc.?

Basically I want the permutations such that each two-element pairing happens only once.

I’ll bet there’s an additional itertool that would solve this but I’m not expert enough to know what it is.

Update: Gareth Rees is correct — I was completely unaware that I was trying to solve the round-robin problem. I have an additional constraint which is that what I’m doing is grouping people for pair-programming exercises. Thus, if I have an odd number of people, I need to create a group of three to include an odd person for each exercise. My current thinking is to (1) make an even number of people by adding in an invisible person. Then, after the pairing, find the person paired with the invisible person and randomly place them into an existing group to form a team of three. However, I wonder if there isn’t already an algorithm or adjustment to round-robin that does this in a better way.

Update 2: Theodros’ solution produces exactly the right result without the inelegant futzing about I describe above. Everyone’s been amazingly helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:22:30+00:00Added an answer on June 16, 2026 at 10:22 pm

    I’d like to share a different implementation of round-robin scheduling that makes use of the deque-data structure from the Standard Library:

    from collections import deque
    
    def round_robin_even(d, n):
        for i in range(n - 1):
            yield [[d[j], d[-j-1]] for j in range(n/2)]
            d[0], d[-1] = d[-1], d[0]
            d.rotate()
    
    def round_robin_odd(d, n):
        for i in range(n):
            yield [[d[j], d[-j-1]] for j in range(n/2)]
            d.rotate()
    
    def round_robin(n):
        d = deque(range(n))
        if n % 2 == 0:
            return list(round_robin_even(d, n))
        else:
            return list(round_robin_odd(d, n))
    
    
    print round_robin(5)
      [[[0, 4], [1, 3]],
       [[4, 3], [0, 2]],
       [[3, 2], [4, 1]],
       [[2, 1], [3, 0]],
       [[1, 0], [2, 4]]]
    
    
    print round_robin(2)
       [[[0, 1]]]
    

    It puts the objects(ints here) in the deque. Then it rotates and builds consecutive pairs taking from both ends towards the middle. One can imagine this as folding the deque in the middle back on itself. To make it clear:

    Case uneven elements:

     round 1     round 2       # pairs are those numbers that sit
    ----------  ---------      # on top of each other
    0 1 2 3 4   8 0 1 2 3
    8 7 6 5     7 6 5 4
    

    In case of even elements an additional step is required.
    (I missed the first time cause I only checked the uneven case. This yielded a horribly wrong algorithm… which shows me how important it is to check edge cases when implementing an algorithm…)
    This special step is that I swap the two leftmost elements (which are the first and last elements of the deque) before each rotation — this means the 0 stays all the time upper left.

    Case even elements:

     round 1     round 2       # pairs are those numbers that sit
    ----------  ---------      # on top of each other
    0 1 2 3     0 7 1 2
    7 6 5 4     6 5 4 3
    

    What haunts me about this version is the amount of code duplication, but I couldn’t find a way to improve while keeping it as readable. Here’s my first implementation, which is less readable IMO:

    def round_robin(n):
        is_even = (n % 2 == 0)
        schedule = []
        d = deque(range(n))
        for i in range(2 * ((n - 1) / 2) + 1):
            schedule.append(
                            [[d[j], d[-j-1]] for j in range(n/2)])
            if is_even:
                d[0], d[-1] = d[-1], d[0]
            d.rotate()
        return schedule
    

    Update to account for the updated question:

    To allow in the uneven case for groups of three you just need to change round_robin_odd(d, n):

    def round_robin_odd(d, n):
        for i in range(n):
            h = [[d[j], d[-j-1]] for j in range(n/2)]
            h[-1].append(d[n/2])
            yield h
            d.rotate()
    

    This gives:

    print round_robin(5)
    [[[0, 4], [1, 3, 2]],
     [[4, 3], [0, 2, 1]],
     [[3, 2], [4, 1, 0]],
     [[2, 1], [3, 0, 4]],
     [[1, 0], [2, 4, 3]]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate Entities/Object from selected tables - not all. Is this possible
I am current working on a project where I need to generate all possible
I have the following need (in python): generate all possible tuples of length 12
I'm trying to implement a class that will generate all possible unordered n-tuples or
I need to generate all possible numbers in prefix: import random prefix = 05
I have a list of words and I need to generate all possible permutations
For given n inputs, I need to generate all possible input combinations using C++
How do I generate all possible combinations of n-bit strings? I need to generate
I want to generate all possible sequence for 49 6 lottery So I need
I need to generate possible ranking of all possible ranking of n documents. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.