Skip to content

Instantly share code, notes, and snippets.

@jMyles
Created June 24, 2023 14:01
Show Gist options
  • Save jMyles/0aef53dd50ff4dab250c6eb894150925 to your computer and use it in GitHub Desktop.
Save jMyles/0aef53dd50ff4dab250c6eb894150925 to your computer and use it in GitHub Desktop.
Sort tokens by choice
bidders = {
'joe': [0, 1, 50],
'sue': [0, 1, 60],
'joan': [0, 1, 10],
'bill': [42, 0, 100],
'john': [42, 1, 50],
'jane': [17, 0, 10]
}
all_desired_token_ids = set()
for bidder, bid_info in bidders.items():
first_choice, second_choice, bid_amount = bid_info
all_desired_token_ids.add(first_choice)
all_desired_token_ids.add(second_choice)
token_ids_to_distribute = list(all_desired_token_ids)
_candidate_token_id = 0
while len(token_ids_to_distribute) < len(bidders):
# There need to be enough tokens to go around.
if _candidate_token_id not in token_ids_to_distribute:
token_ids_to_distribute.append(_candidate_token_id)
_candidate_token_id += 1
token_ids_to_distribute.sort()
bidders_sorted_by_bid = sorted(bidders.items(), key=lambda x: x[1][2], reverse=True)
winners = {}
for bidder, bid_info in bidders_sorted_by_bid:
first_choice, second_choice, bid_amount = bid_info
if first_choice in token_ids_to_distribute:
token_ids_to_distribute.remove(first_choice)
winners[first_choice] = bidder
elif second_choice in token_ids_to_distribute:
token_ids_to_distribute.remove(second_choice)
winners[second_choice] = bidder
else:
# This bidder is out of luck.
default = token_ids_to_distribute.pop(0)
winners[default] = bidder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment