Getting Runtime error in codejam but works fine elsewhere

77 views Asked by At

I wrote my code in python 3. It works perfectly fine on the python ide and programiz online ide but gives RUNTIME error on the google platform. can someone help out as to why this is happening? Here's the link to the problem:Vestigium

def main(x,n,N):
n = len(x)
r = 0
for i in x:
    if len(i)> len(set(i)):
        r += 1
cols = []
k = 0
for i in range(n):
    col = []
    for j in range(n):
        col.append(x[i][j])
        if i == j:
            k += int(x[i][j])
    cols.append(col)
c = 0
for i in cols:
    if len(i)> len(set(i)):
        c += 1
print("Case #",N,":",k,r,c,)


  
 N = int(input())
    for i in range(N):
        x = []
        n = int(input())
        for j in range(n):
            row = []
            for p in range(n):
                v = int(input())
                row.append(v)
            x.append(row)
        num = i + 1
        main(x,n,num)
1

There are 1 answers

0
Abhinav Mathur On
  1. You're handling the inputs incorrectly. You call input() for each element of a row, which gives an error. You're supposed to handle the entire line as one input. Replace
for j in range(n):
       row = []
       for p in range(n)
             v = int(input())
             row.append(v)
       x.append(row)

With

for j in range(n):
        row = list(map(int, input().split()))
        x.append(row)
  1. Try running this statement in an IDE:
print("Case #",1,":",1)

This prints Case # 1 : 1. The program clearly specifies the output to be in Case #1: 1 format. The extra space between # and case number causes an error. Using Python 3 and above, use an f-string to make it easier.

print(f"Case #{N}: {k} {r} {c}")

This should fix the Runtime Error. I still suspect it will give a wrong answer though.