1 / 7
Jun 2011

Hi. I have been having trouble doing the basic TEST task in Python3. I always get a NZEC if I use exit() (no matter if I specify the exit code or not):

from sys import stdin
for line in stdin:
  if line.startswith('42'):
    exit(0)
  else:
    print(line)

If I change exit(0) to break it works. There are also no such problems with python2 and exit(0) - you can literally take the code I pasted above as it doesn't use any P3 features.

Why does the exit() in Python3 cause the program to fail always? And why does it work in Python 2?

After all, Python 2 seems much faster (0.02 vs 0.12) so I will probably go on using this one, but I like to know such things ;d

A side question - does the engine take languages / runtimes into account, and adapt memory / time limits to it? So that a submission in Python can take more mem / time than in C?

Regards.

  • created

    Jun '11
  • last reply

    Jun '11
  • 6

    replies

  • 362

    views

  • 2

    users

  • 1

    link

Read the last post of this thread: https://www.spoj.pl/forum/viewtopic.php?f=3&t=841115

Yes, Python 2.5 is faster, but nevertheless you can solve hundreds of SPOJ problems using Python 3.

No.

This means certain tasks /languages combos are condemned to fail right on the spot, when the language is chosen. And that a better solution (algorithm) coded in, say, Ruby, could still take longer than an inferior one in C, and even fail. Right?

Thanks for other answers.

There appear discussions of this kind in the forum from time to time, but it seems there is no solution that takes all considerable aspects into consideration. Indeed there are some problems that are impossible to solve with several "slower" languages because of strict time limit. On the other hand there are some problems in the challenging section, where some slower languages (escpecially Ruby, Perl, Python, sometimes Bash) are very powerful and get the first ranks. For classical section runtime doesn't matter for your scoring as long as you don't get a TLE.

Anyway Python is a good choice as you can solve hundreds (probably thousands) of SPOJ problems using Python. And: Python isn't that slow if you do it right, you have builtin bigint support, a huge standard lib, powerful data structures and rapid development.

Ok, I would have another question if I may ;d
Task 400 (To and fro):

import sys
def tofrom():
	while 1:
		cols = int(sys.stdin.readline())
		if cols == 0:
			return
		code = sys.stdin.readline().rstrip()
		rows = len(code) // cols
		plain = bytearray(len(code))
		for i in range(rows):
			for j in range(cols):
				if i % 2 == 1:
					k = cols - j - 1
				else:
					k = j
				plain[i + k * rows] = ord(code[i * cols + j])
		print(plain.decode('ASCII'))
tofrom()

I keep getting NZEC in Python 2.5 - what is wrong here this time? Am I doing something wrong again?
Is there some tutorial describing the exact rules that my code has to comply with?

(I have been bombing the engine with my attempts in the last 15 minutes or so, I hope I don't get banned ;d)

Regards.

Python 2.5.5 (r255:77872, Jan 31 2010, 15:34:35) 
>>> bytearray
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'bytearray' is not defined