Python NZEC error TTO1

import math
import os
import random
import re
import sys

try:
if name == ‘main’:
n = int(input())

    ar = list(map(int, input().strip().split()))

    for i in range(n):
        cap=1
        for j in range(n - 2):
            if ar[j] > ar[j + 2]:
                t = ar[j + 2]
                ar[j + 2] = ar[j]
                ar[j] = t

            else:
                cap+=1

        if cap+2==n:
            break

    for i in range(len(ar)-1):
        if ar[i] > ar[i + 1]:
            print('1')
            exit()
        else:
            continue

    print('OK')

except EOFError:
exit

can someone please figure why I’m getting NZEC error. I noob to CP, please help me.

Written on the contest page
Please do not discuss strategy, suggestions or tips in the comments during a live contest.
You will probably find a similar statement on every contest on Codechef.

Contest will be over in 4 hours.

Some advice for future posts:

  1. Don’t just copy paste your code in the description. Provide a submission link or ideone.com link instead.
  2. Provide a link to the problem too.
  3. Explain briefly what you have done. It will make it much easier for people to help you.

You can edit your post.

Sorry but I didn;t know this link thing. But now from next time I’ll try to follow the code of conduct of the competition

Everything does look to be correct.

Remove the unnecessary import statements. It is possible that Codechef may not have some of those libraries (unlikely).
Remove the if __name__ == 'main'
Also, it should be print(i) instead of print('1')

This code looks like it has been converted to python from C using some converter (70% sure about this). Please don’t use such things especially in a contest that has relatively easy problems so that you can learn.

I agree about the library stuff but sorry to say. I never learnt C language but I don’t know how you’re 70% sure. Morever print(‘1’) was required to be printed over other not ‘i’.
Btw thanks for suggestion, next time I’ll try to not to add unnecssasry libraries.

I don’t know how you’re 70% sure

Sorry for that. It’s just that I’ve seen python ‘templates’ on HackerRank and they also had unnecessary imports that matched exactly with the unnecessary #includes in their C++ templates. A lot of other things were not written in ‘pythonic’ way so that made me believe that those HackerRank templates were lazily converted.
Your code is was very similar to one of those templates that’s why I said that.

Morever print(‘1’) was required to be printed over other not ‘i’.

From the problem page:
Output in single line “OK” (without double quotes) if Triplet Bubble Sort correctly sorts the list, or the index (counting starting from 0) of the first sorting error, as described in the problem.

So, i in your loop is the index of the first sorting error. Also note the difference between i and 'i'

It is possible that there is a test case with n = 1 and since your code prints ‘1’, the runtime error is actually in the checker for the problem. Try submitting after removing the imports and the '1'