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]: TSORT Problem - CodeChef

Did you try removing the psyco stuff.

2 Likes

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 FAQ | CodeChef. 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

CodeChef: Practical coding for everyone (mine)
CodeChef: Practical coding for everyone (yours)

1 Like

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

1 Like