After some thinking, I've taken a stab at converting your algorithm to my data, and this is what I arrived at. The result forconverting the second testcase is still differentlist-based approach to a dict-based one
parsing people
changed the input to a lookup table:
def parse_people(input, people):
for _ in range(people):
person, *prefs = map(int, input().split())
yield person, {k: i for i, k in enumerate(prefs)}
def find_partners(people, prefs_women, prefs_men):
from collections import OrderedDict
choices_women = OrderedDict(((key, None ) for key in prefs_women))
choices_men = OrderedDict(((key, None) for key in prefs_men))
for k in choices_men:
hub = k
while(hub is not None):
hub = find_woman(choices_women, choices_men, hub, prefs_women, prefs_men)
# print(hub, choices_women)
return choices_women, choices_men
if(__name__ == "__main__"):
input = iter(input_str.split('\n')).__next__
from collections import OrderedDict
test_cases = int(input())
for _ in range(test_cases):
people = int(input())
women = OrderedDict(parse_people(input, people, 'woman', 'man'))
men = OrderedDict(parse_people(input, people, 'man', 'woman'))
# print('-----preferences: ', women, men)
choices_women, choices_men = find_partners(people, menwomen, womenmen)
for man, woman in choices_men.items():
print(man, ' ', woman)
1 3
2 2
3 1
4 4
1 4
2 5
3 1
4 3
5 7
6 6
7 2
bughunting
Since the names of the men and women are the same, to hunt for a bug I changed the parse_people temporary to this:
def parse_people(input, people, sex1='man', sex2 = 'woman'):
for _ in range(people):
# person, *prefs = map(int, input().split())
person, *prefs = input().split()
# yield person, {k: i for i, k in enumerate(prefs)}
yield sex1 + '_' + person, {sex2 + '_' + k: i for i, k in enumerate(prefs)}
for a more verbose output. This way I found out I had changed the order of women and men somewhere