-
-
Save roSievers/6e954a1c64c6b065f22e to your computer and use it in GitHub Desktop.
This file contains 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
import math | |
def distributionWithNPirates(n): | |
if (n == 1): # Base case | |
return [100] | |
numberOfVotesRequired = int(math.ceil(n / 2.0)) | |
# Pirate n will vote for his proposal regardless | |
numberOfVotesRequired -= 1 | |
distributionIfPirateNKilled = distributionWithNPirates(n - 1) | |
piratesThatGetTheLeastIfPirateNIsKilled = sorted(range(len(distributionIfPirateNKilled)), key=distributionIfPirateNKilled.__getitem__) | |
minimumDistribution = [0] * (n - 1) | |
totalBribes = 0 | |
for pirateToBribe in piratesThatGetTheLeastIfPirateNIsKilled[:numberOfVotesRequired]: | |
bribeAmount = distributionIfPirateNKilled[pirateToBribe] + 1 | |
minimumDistribution[pirateToBribe] = bribeAmount | |
totalBribes += bribeAmount | |
remainderForPirateN = 100 - totalBribes | |
if remainderForPirateN < 0: | |
# This pirate has no chance of survival. This is denoted by a -1 | |
# The distribution to the other pirates does not change, since | |
# the proposal of the pirate can't pass anyway | |
distributionIfPirateNKilled.append(-1) | |
return distributionIfPirateNKilled | |
minimumDistribution.append(remainderForPirateN) | |
return minimumDistribution | |
import sys | |
n = int(sys.argv[1]) | |
print distributionWithNPirates(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment