ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1510. Order

Python 3.4 "Time limit exceeded" test№19 What's wrong?
Posted by Danya 22 Oct 2015 01:30
What is the test №19?

""
from sys import stdin
from collections import Counter

def search():
    tmp = Counter([int(x) for x in stdin])
    return [i for i in tmp.keys() if tmp[i] == max(tmp.values())][0]
search()
"""

Edited by author 22.10.2015 01:31
Re: Python 3.4 "Time limit exceeded" test№19 What's wrong?
Posted by Alexander Vasilyev`~ 13 Jun 2018 20:51
Maybe replace search() with print(search())?
That was a joke, but when I tried to submit this code, i got WA1.
The test is ok, but your code is not optimal. Your code calculates max(tmp.values()) every time, and it is a costly operation.
And here is my optimized (WA23, because there is another drawback in your solution) version of it:
from sys import stdin
from collections import Counter

def search():
    tmp = Counter(map(int, stdin))
    on2_to_on = max(tmp.values())
    print(next((i for i in tmp.keys() if tmp[i] == on2_to_on)))
search()
Or:
from sys import stdin
from collections import Counter

def search():
    tmp = Counter(stdin)
    print(tmp.most_common(1)[0][0])
search()
But with 1 additional line I got AC. Btw, the Python is relatively slow and uses a bit more memory too.