Created
June 30, 2015 15:46
-
-
Save lukmdo/3e7022e0d35068dcf608 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 solution(items): | |
if not items: | |
return | |
best = None | |
best_count = 0 | |
current_count = 0 | |
last = items[0] | |
for current in items: | |
if current == last: | |
current_count += 1 | |
else: | |
current_count = 1 | |
last = current | |
if current == best: | |
best_count = current_count | |
elif current_count > best_count: | |
best = current | |
best_count = current_count | |
if best_count > len(items) // 2: | |
return best | |
def test_solution(): | |
assert solution(sorted([])) == None | |
assert solution(sorted([1])) == 1 | |
assert solution(sorted([1, 2])) == None | |
assert solution(sorted([1, 2, 3])) == None | |
assert solution(sorted([1, 2, 1])) == 1 | |
assert solution(sorted([1, 2, 1, 2])) == None | |
if __name__ == "__main__": | |
test_solution() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment