I am creating a program which takes a file with a new 'thing' on each line. For testing, I am using NBA basketball teams (which there are 30 of). It asks the person A vs. B, until it can create a full list of their favourite to least favourite 'thing'.
So far, this works a charm, but asks too many questions. I have read and understood an answer regarding binary insertion sort, found here: https://stackoverflow.com/a/33748286/8419835 However, I am having trouble implementing it with how I currently have my code structured.
options = []
toPrint = []
with open("list.txt", "r") as f:
for line in f.read().splitlines():
options.append(dict(name=line,superiors=[],inferiors=[],active=False))
print()
for o in options:
o['active'] = True
for c in options:
if c != o and c['active'] and o['name'] not in c['superiors'] and o['name'] not in c['inferiors'] and c['name'] not in o['superiors'] and c['name'] not in o['inferiors']:
choice = input(o['name'] + ' (1) or ' + c['name'] + ' (2) ? : ')
if choice == '2':
c['inferiors'].append(o['name'])
c['inferiors'].extend(o['inferiors'])
o['superiors'].append(c['name'])
o['superiors'].extend(c['superiors'])
c['inferiors'] = list(set(c['inferiors']))
o['superiors'] = list(set(o['superiors']))
else:
o['inferiors'].append(c['name'])
o['inferiors'].extend(c['inferiors'])
c['superiors'].append(o['name'])
c['superiors'].extend(o['superiors'])
o['inferiors'] = list(set(o['inferiors']))
c['superiors'] = list(set(c['superiors']))
print()
for x in range(30):
for o in options:
if len(o['superiors']) == x:
toPrint.append(o['name'])
for x in range(30):
print(str(x + 1) + '. ' + toPrint[x])
print()
Does anyone have any ideas on how I can take the code I currently have, and modify it so that it asks the minimum possible amount of questions, as seen in the link above?