Created
November 2, 2016 05:08
-
-
Save iconjack/7b1e2d6cd69f078df5d842b0a4356bcc to your computer and use it in GitHub Desktop.
Determine how many ways the US election could end in electoral tie
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
# Determine how many ways the US election could end in electoral tie. | |
# Respsonse to https://twitter.com/daveinstpaul/status/792788962230226944 | |
from functools import lru_cache | |
votes = [9,8,3,3,8,18,11,4,7,6,10,7,55,11,20,9,16,4,7,10,9,3,6,3,3,10, | |
11,29,3,38,16,5,6,4,6,3,4,4,13,20,14,12,11,5,5,6,29,10,6,15,3] | |
@lru_cache(maxsize=10000) | |
def count_ties(subtotal, votes): | |
if subtotal > tiescore: return 0 | |
if len(votes) == 0: return subtotal == tiescore | |
return count_ties(subtotal+votes[0], votes[1:]) + count_ties(subtotal, votes[1:]) | |
votes = tuple(votes) # need cacheble for @lru_cache | |
assert sum(votes) % 2 == 0 | |
tiescore = sum(votes)/2 | |
print (count_ties(0, votes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment