NZEC error in Python

I am new to python, and can’t figure out why am I getting NZEC error. I am using Python 2.7

The problem is [Turbo Sort][1]

import sys
import psyco
 
psyco.full()
 
def main():
	sys.stdin.readline()
	a = []
	for line in sys.stdin:
		n = int(line)
		a.append(n)
		
	a.sort()
	for i in a:
		sys.stdout.write(str(i))
		sys.stdout.write('\n')
 
if __name__ == "__main__":
	sys.exit(main()) 

Without ‘sys’, I am getting TLE, because of enormous I/O.
[1]: http://www.codechef.com/problems/TSORT

Did you try removing the psyco stuff.

1 Like

sys.exit(main())

Replace this line with main() and you’re program should run!
The main reason you got a TLE was because of these lines :

for line in sys.stdin:
    n = int(line)
    a.append(n)

replace it with :

a= map(int, sys.stdin) 

This will make you’re program much faster since its mapping the input directly into the array from stdin rather than appending it.

1 Like

replace these two statements:

sys.stdout.write(str(i))
sys.stdout.write('\n')

with

print i

and it gets AC

2 Likes

Now I tried without psyco, but it gave TLE. I used psyco, becuase it is mentioned here http://www.codechef.com/wiki/faq#Why_do_I_get_a_Time_Limit_Exceeded. I think I will do it in C instead.

+1 To all the answers. I had to follow all 3 advises to get an AC. Thanks :slight_smile:

Thanks finally got AC :slight_smile:

:-/ Try to compare these two submissions, you got TLE, I got AC

http://www.codechef.com/viewsolution/1479153 (mine)
http://www.codechef.com/viewsolution/1479219 (yours)

1 Like

Discrimination! :wink: Sadly the time limit has to be made tight.

1 Like