Created
October 7, 2016 14:57
-
-
Save teldridge11/835a7b18d7b8d74bca214dfdb6a2f315 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
def majority(array, tiebreaker=None): | |
n = len(array) | |
if n == 0: | |
return tiebreaker | |
pairs = [] | |
if n % 2 == 1: | |
tiebreaker = array[-1] | |
for i in range(0, n-1, 2): | |
if array[i] == array[i+1]: | |
pairs.append(array[i]) | |
major = majority(pairs, tiebreaker ) | |
if major is None: | |
return None | |
nMajor = array.count(major) | |
if 2*nMajor > n or (2*nMajor == n and major == tiebreaker ): | |
return major | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment