Skip to content

Instantly share code, notes, and snippets.

@prophile
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prophile/ec2adf0ac8d1a7f00856 to your computer and use it in GitHub Desktop.
Save prophile/ec2adf0ac8d1a7f00856 to your computer and use it in GitHub Desktop.
Resource distribution brute-force algorithm in Python
from itertools import permutations
import yaml
import sys
#votes = {'a': ['cheese', 'hats', 'faces'],
# 'b': ['hats', 'cheese', 'faces'],
# 'c': ['hats', 'faces', 'cheese']}
votes = {'a': 'CHWESK',
'b': 'CESWHK',
'c': 'HCWSEK',
'd': 'SKEWHC',
'e': 'CHEWSK',
'f': 'EHWCSK'}
def brute(votes, utilitarianism=2):
ballots = list(votes.values())
if not ballots:
return {}
def cost(permutation):
return sum((ballot.index(choice) - 1) ** utilitarianism
for choice, ballot in zip(permutation, ballots))
selection = min(permutations(sorted(ballots[0])),
key=cost)
return dict(zip(votes.keys(), selection))
yaml.dump(brute(votes, utilitarianism=3), sys.stdout, default_flow_style=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment