Skip to content

Instantly share code, notes, and snippets.

@lukmdo
Created June 30, 2015 15:46
Show Gist options
  • Save lukmdo/3e7022e0d35068dcf608 to your computer and use it in GitHub Desktop.
Save lukmdo/3e7022e0d35068dcf608 to your computer and use it in GitHub Desktop.
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