How to calculate the number of 4 man teams and 3 man teams from a list of pairs and singles

I’m trying to code a random team selection program. The inputs are a list of pairs and a list of singles. The output should be a random list of teams with either 3 or 4 members, minimizing the number of 3 man teams.

Given pairs = [(a,b), (c,d), (e,f), (g,h)]
and singles = [x, y, z]
then teams might be [ a, b, g, h]; [c, d, x, z] and [e, f, y]

while
Given pairs = [(a,b), (c,d), (e,f), (g,h), (i,j)]
and singles = [x, y, z]
then teams might be [ c, d, i, j]; [e, f ,y]; [a, b, x] and [i, j, z]

What I need is code to calculate the number of 4 man teams and the number of 3 man teams where the number of 3 man teams is minimized.

Thanks. If this topic is not allowed I will remove it.

Welcome to the Python Discourse! So the purpose of the Python Help category, as with most help forums (aside from paid cheating sites) is to help you code, not do the coding for you, which would defeat the intended purpose of the exercise of helping you learn. For us to do so, we need to know what you’ve tried so far, what happened when you did, where you’re getting stuck and what specific issues you’re running in to, so we can give you useful pointers.

For starters, when considering a problem it’s important to pay careful attention to what is being asked, and under what constraints. The above problem statement contains mutually incompatible inconsistencies you’ll want to be clear on; e.g. it first states

I.e… you want an output of a list of lists of members of each team, but then you say

I.e. you want an output of simply the number of three vs four person teams.

The latter merely requires a bit of mathematics to figure out the numbers, while the former also entails then constructing the lists in question.

I can create the pairs and singles lists ok, but I haven’t come up with an efficient way to maximize the number of complete (4 man) teams. I’ve even tried Chat GPT but it came up with an incorrect solution.

Thanks for the reply.
I’ve now figured out an algorithm that will work.
I was lost trying to figure out an equation that would do the trick.

I’m coding for a golf group that has couples that play and singles that play but the number playing varies from week to week.

I haven’t done any coding for 15 years but thought it would be fun to automate the team selection.
I want to also minimize repeat pairings from week to week.

1 Like