1

I'm trying to solve a problem on Sphere Online Judge(SPOJ) where it requires me to print all the integers between 1 and n that are divisible by x but not y, My code is right when I test on the Python IDE but when I try to input it on SPOJ, I get runtime error(NZEC), what is NZEC and why do I get it? This is my code:

test_cases = raw_input()
input = []
list = []
for z in xrange(int(test_cases)):
    input = raw_input()
    n,x,y = input.split(' ')
    for z in xrange(int(n)):
        if z%int(x) == 0 and z%int(y) != 0:
              list.append(z)
    answer1 = str(list).strip('[]')
    answer2 = answer1.replace(',', '')
    print answer2
2
  • 1
    For future reference an "NZEC" error is "Non Zero Exit Code". Probably because of an Exception. Commented Feb 7, 2014 at 21:50
  • @NickT Sometime what happen that when problem setter creates a problem , he generate a input test file in which he left a space after every input line , So instead of using input.split(' '), use input.split(). Example 2 3 4 Test file contain space after 4 , i.e, we get NZEC, so use input.split() instead of input.split(' ') Commented Feb 8, 2014 at 21:36

1 Answer 1

1

Are you perhaps talking about this problem?

I see a few issues in your code:

  1. You are getting NZEC because perhaps there is an empty line in the input which you haven't taken into consideration. (See below for trivial modification to address this)

  2. Also, your code has a bug because z starts from 0 through n-1, whereas z should be > 1, so z should be in xrange(2,int(n))

  3. You don't need to strip, and then replace comma with space. You can do that in a single go (see below)

I have modified your code a little bit and it passes the test cases.

def get_line():
    while True:
        line = raw_input().rstrip()
        if not line:
            pass
        else:
            return line

test_cases = get_line()
for _ in xrange(int(test_cases)):
    input = get_line()
    n,x,y = [int(z) for z in input.split(' ')]
    list = []
    for z in xrange(2,n):
        if z%x == 0 and z%y != 0:
              list.append(z)
    answer1 = ' '.join(map(str,list))
    print answer1
Sign up to request clarification or add additional context in comments.

1 Comment

Oh okay, thank you very much for helping and simplifying my code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.