Last active
February 5, 2023 17:19
-
-
Save HFTrader/4c23fa9dd2cf4afc5d05c2657d81a6a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ['Mary', 'Joe', 'Bill', 'Annie', 'Jorge'] = 205 Key votes: [] | |
| ['Mary', 'Joe', 'Bill', 'Annie'] = 125 Key votes: ['Bill', 'Annie'] | |
| ['Mary', 'Joe', 'Bill', 'Jorge'] = 135 Key votes: ['Jorge'] | |
| ['Mary', 'Joe', 'Annie', 'Jorge'] = 175 Key votes: ['Jorge'] | |
| ['Mary', 'Joe', 'Jorge'] = 105 Key votes: ['Mary', 'Joe', 'Jorge'] | |
| ['Mary', 'Bill', 'Annie', 'Jorge'] = 185 Key votes: [] | |
| ['Mary', 'Bill', 'Annie'] = 105 Key votes: ['Mary', 'Bill', 'Annie'] | |
| ['Mary', 'Bill', 'Jorge'] = 115 Key votes: ['Bill', 'Jorge'] | |
| ['Mary', 'Annie', 'Jorge'] = 155 Key votes: ['Annie', 'Jorge'] | |
| ['Joe', 'Bill', 'Annie', 'Jorge'] = 200 Key votes: [] | |
| ['Joe', 'Bill', 'Annie'] = 120 Key votes: ['Joe', 'Bill', 'Annie'] | |
| ['Joe', 'Bill', 'Jorge'] = 130 Key votes: ['Bill', 'Jorge'] | |
| ['Joe', 'Annie', 'Jorge'] = 170 Key votes: ['Annie', 'Jorge'] | |
| ['Bill', 'Annie', 'Jorge'] = 180 Key votes: ['Jorge'] | |
| ['Bill', 'Jorge'] = 110 Key votes: ['Bill', 'Jorge'] | |
| ['Annie', 'Jorge'] = 150 Key votes: ['Annie', 'Jorge'] | |
| Owners: {'Mary': 5, 'Joe': 20, 'Bill': 30, 'Annie': 70, 'Jorge': 80} | |
| Power: {'Mary': 2, 'Joe': 2, 'Bill': 6, 'Annie': 6, 'Jorge': 10} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from itertools import combinations,permutations,product | |
| # Inputs: a map between owners and amount of stocks they own and vote modes | |
| stocks = { "Mary":5, "Joe":20, "Bill":30, "Annie":70, "Jorge":80 } | |
| vote_types = ["Y","N"] | |
| # Separate arrays for easier processing later | |
| names = list(stocks.keys()) | |
| total = sum(stocks.values()) | |
| # Create a dictionary to count voting power for each individual | |
| voting_power = dict([ (name,0) for name in names ]) | |
| # Create a table with all the voting possibilities | |
| vote_table = [ vote_types for name in names ] | |
| # Iterate over all the possible outcomes | |
| for votes in product( *vote_table ): | |
| # Find the index of everyone who voted yes | |
| votes_yes = [ name for name,vote in zip(names,votes) if vote=='Y' ] | |
| # Get the vote ownership of those who voted yes and compute the total | |
| stocks_yes = [ stocks[name] for name in votes_yes ] | |
| total_votes = sum(stocks_yes) | |
| # Check if the vote was enough to win | |
| if total_votes >= total/2: | |
| # For each person who voted yes, remove them and see if it still passes | |
| votes_plus = [] | |
| for name in votes_yes: | |
| partial_sum = total_votes - stocks[name] | |
| if partial_sum < total/2: | |
| # If it does not pass, this person is important, has vote power | |
| voting_power[name] += 1 | |
| votes_plus.append( name ) | |
| # Print the names of the voters, the total of votes and the names of who got a plus | |
| print( votes_yes , '=', total_votes, " Key votes:", votes_plus ) | |
| # Print the results | |
| print( "\nOwners: ", stocks ) | |
| print( "Power: ", voting_power ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment