I like programming challenges and trying to solve them, however, this one has me a bit stumped and I could really use a point in the right direction.
There is a set of numbers, 1 to N that represent females and N+1 to 2N that represent male. (So if N = 5 then that means 1,2,3,4,5 = female and 6,7,8,9,10 = male.) I am trying to find the maximum amount of matches possible. Here's the catch though, the female numbers are picky and will only pair with numbers on their line. So it looks like this:
19
1: 25
2: 20 25 28
3: 27 32 37
4: 22
5: 32 38
6: 32 34 35
7: 22 34 37
8: 30 35 38
9: 20 23
10: 24 29
11: 29 32
12: 23 26 31
13: 21 25 34
14: 21 27
15: 20
16: 23 31 38
17: 22 27 28
18: 35
19: 24 25
So where "#: " is the girl, you'd say that girl #1 will ONLY pair with guy #25. Likewise, girl #2 will ONLY pair with guys #20,25, or 28.
My solution to this was to make each line into an array and then also make an empty array of values already used. I started with girls who would only pair with one person and gave them the pair. Then I went onto girls who would only pair with someone out of a group of 2. Then finally 3.
This worked decently, however, this meant some people didn't get paired when they could have been, thus my maximum amount of pairs possible wasn't reached due to the first-come-first-serve nature of it.
Using the example above, I got 16 out of 19 (where the maximum possible is 17.) with the 3 non-paired girls being #13, 17, and 19.
Here's is my current code:
def findPairs(women):
pairs = []
taken = []
for woman in range(women):
data = raw_input()
#Remove the "#: " in each line and format numbers into ['1','2','3']
data = (data[len(str(woman))+2:]).split()
pairs.append(data)
for entry in pairs:
if len(entry) == 1:
if entry[0] not in taken:
taken.append(entry[0])
else:
print("No match for " + str(entry))
for entry in pairs:
if len(entry) == 2:
if entry[0] not in taken:
taken.append(entry[0])
elif entry[1] not in taken:
taken.append(entry[1])
else:
print("No match for " + str(entry))
for entry in pairs:
if len(entry) == 3:
if entry[0] not in taken:
taken.append(entry[0])
elif entry[1] not in taken:
taken.append(entry[1])
elif entry[2] not in taken:
taken.append(entry[2])
else:
print("No match for " + str(entry))
print(len(taken))
findPairs(input())
Does anyone have any idea how I can better optimize my pair matching so that I can reach maximum pairs? (Preferably a way that can expand as well, finding a hack-ish way to find the answer for these 19 isn't too rewarding if it doesn't work for a different set of say 50 girls. It's fine to assume however the format will stay the same (ie: 3 guys max per line, lesser to greater order, etc..) although it would be cool to be able to make it as expandable as possible, that's besides the point and beyond the reach of this question.)