Cod sursa(job #2510549)

Utilizator mehanixCiausu Nicoleta mehanix Data 16 decembrie 2019 21:21:09
Problema Elementul majoritar Scor 50
Compilator py Status done
Runda Arhiva educationala Marime 0.83 kb
#n a vazut planeta asa ceva
def generator(path):
    word = ''
    with open(path) as file:
        while True:
            char = file.read(1)
            if char.isspace():
                if word:
                    yield word
                    word = ''
            elif char == '':
                if word:
                    yield word
                break
            else:
                word += char


nr = generator("elmaj.in")

n= next(nr)
g = open("elmaj.out","w+")
#print(t)

dict = {}
ok = 0
maxel=[0,0]
for a in nr:
    if a not in dict:
        dict[a]=1
    else:
        dict[a]+=1

for k,v in dict.items():
    if v>maxel[1]:
        maxel[0] = k
        maxel[1] = v

if maxel[1] > int(n)//2:
    g.write(maxel[0] + " " + str(maxel[1]) + '\n')
else:
    g.write("-1\n")
g.close()