I am new tro programming and have chosen python 2.7 to begin with. But, I often have to deal with a TLE while competitive coding like in the problem below, where my code runs fine on all test cases but runs out of the given time bound.
My code runs fine on Ideone.com for all the given test cases, but gives a TLE on SPOJ.
This is the SBANK problem at SPOJ.
In one of the internet banks thousands of operations are being performed every day. Since certain customers do business more actively than others, some of the bank accounts occur many times in the list of operations. Your task is to sort the bank account numbers in ascending order. If an account appears twice or more in the list, write the number of repetitions just after the account number. The format of accounts is as follows: 2 control digits, an 8-digit code of the bank, 16 digits identifying the owner (written in groups of four digits), for example (at the end of each line there is exactly one space):
30 10103538 2222 1233 6160 0142
Banks are real-time institutions and they need FAST solutions. If you feel you can meet the challenge within a very stringent time limit, go ahead! A well designed sorting algorithm in a fast language is likely to succeed.
My code as follows :
def neww(k): neww=sorted(set(x+"%d"%(k.count(x)) for x in k)) for each in neww: print each for _ in range(int(raw_input())): p=raw_input() if p=='': p=raw_input() k=[raw_input() for _ in xrange(int(p))] neww(k) print ""