Created
January 17, 2016 14:26
-
-
Save AlexMikhalev/498313db3f57e6a47045 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
from collections import defaultdict | |
from operator import itemgetter | |
def solution(A): | |
# write your code in Python 2.7 | |
appearances = defaultdict(int) | |
N=len(A) | |
for each_key in A: | |
appearances[each_key] += 1 | |
if not appearances: | |
return -1 | |
max_elem=max(appearances.iteritems(), key=itemgetter(1)) | |
if max_elem[1]>(N/2): | |
for k,v in enumerate(A): | |
if v==max_elem[0]: | |
return k | |
else: | |
return -1 | |
if __name__ == '__main__': | |
print 'Start tests..' | |
assert solution(A) in [0, 2, 4, 6, 7] | |
assert solution([2,1,3,1,4,1,5,1])==-1 | |
assert solution([]) == -1 | |
print 'Ok!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment