Runtime Error for 'COOMILK' on CodeChef (Python 3.4) - python

Hi I am new to Python programming as well as here. I am practicing on www.codechef.com but I am not able to find the solution for the error on my code.
I am using Python 3.4, I am trying to solve this problem https://www.codechef.com/problems/COOMILK
While my code is running perfectly on IDLE it is giving a runtime error when I am trying to submit my code. I tried all the searches and first fixed the EOFerror which I was getting on compilation but now I am not able to fix this Runtime error.
T = int(input())
for loop in range(T):
failed = False
N = int(input())
activities = input().split()
for check in range(N):
if len(activities)==1 and activities[check]== 'cookie':
print ("NO")
failed = True
elif activities[check] == 'cookie' and activities[check+1]!= 'milk':
print ("NO")
failed = True
if not failed:
print("YES")
It would be great if someone help me out in finding out the problem, I have already checked the code with providing the input as required and it is giving desired output but somehow is giving runtime error on submission.
I will appreciate suggestions on improving my code.
Thank you :)

Related

How to get rid of run time error on code chef

The following code works fine on both the jupiter notebook and the pycharm but its showing runtime error on codechef. The code is to check whether the permutation is ambiguous or not.
for i in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
p=l[:]
for j in range(n):
p[l[j]-1]=j+1
if(p==l):
print("ambiguous")
else:
print("not ambiguous")
Welcome to Stack Overflow!
When posting, it usually helps to add more information, such as the error message itself. Regardless, I pasted your code into Python and I've deduced that the problem arises from the input() statement- I'm not sure what 'code chef' is, but it probably doesn't know that the user should input an integer into the input() prompt.
Therefore, a workaround is to hardcode your inputs as variables:
input1 = "5"
input2 = ...
for i in range(int(input1)):
n=int(input2)
...

I am getting EOF error when I upload a code in codechef. How to solve this?

I am getting EOF error when I upload a code in codechef. That same code is running nice without any error in online compilers and Pycharm. I don't know why I am getting this.
a=list(map(int, input().split()))
if a[0]>a[1]:
print(float(a[1]))
elif a[0]%5==0:
print(float(a[1]-(a[0]+0.5)))
else:
print(float(a[1]))
It is a common problem with codechef..
Try the following:
try:
a=list(map(int, input().split()))
if a[0]>a[1]:
print(float(a[1]))
elif a[0]%5==0:
print(float(a[1]-(a[0]+0.5)))
else:
print(float(a[1]))
except:
pass
It is a bit hacky but should solve the problem!

EOF error python 3

def reverse_number(x):
return x[::-1]
number_of_sums = int(input())
for i in range(number_of_sums):
s1 = input().split()
print(int(reverse_number(s1[0])) + int(reverse_number(s1[1])))
I have the following code, which should be a solution to a SPOJ problem. It compiles fine and works for examples that I provide, but as soon as I submit it says that it found an EOF error.
It points me to line 5 in the code, and I believe I understand why. When I use input() I try to get the whole line, right? So, how would I go ahead to only get the number? If that is the problem of course, it might be something else that ruins the code.
def reverse_number(x):
return x[::-1]
try:
number_of_sums = int(input())
for i in range(number_of_sums):
s1 = input().split()
if len(s1) > 0:
print(int(reverse_number(s1[0])) + int(reverse_number(s1[1])))
except ValueError:
print("Invalid Input")
Now, if you run the program and submit it will throw error stating "Invalid Input". Apart from adding the try block, also included the condition check for s1 as you might encounter an "Index out of range" error if not given proper input.
It may be that the grader/engine that checks the code is running Python2, in which case you could change to:
number_of_sums = int(raw_input())
before submitting (and a similar change on line 8).

Getting NZEC error for python:

I am a newbie in python, started learning the language a week ago tried to test it out in codechef...
Here is the question link: https://www.codechef.com/problems/COOMILK
So with what i understood so far i typed the following piece of code..
t=int(input())
f=[]
if(t >= 1 and t <= 50):
for each in range(t):
n=int(input())
if(n >= 1 and n <=50):
m=input().split()
for i in range(len(m)):
if(m[i]=='milk'):
q=1
elif(len(m)==1 and m[0]=='cookie'):
q=0
break
elif(m[i]=='cookie' and m[i+1]!='milk'):
q=0
break
f.append(q)
for i in f:
if(i==1):
print("YES")
elif(i==0):
print("NO")
So apart from the n constraint being not checked during the input, what actually showed was a runtime error.
New to this can someone explain it clearly why i am getting the nzec error?
Compiled it on my own and for all the test cases it worked properly but does not compile on code chef.. any help would be appreciated.
What i have gathered from the net is that there is no raw_input in python 3.x that i am using currently, so just a friendly reminder here....
NZEC stands for "Non Zero Exit Code". It occurs when you encounter a runtime error usually index out of bound error.
There is a mistake in the 14th line of your code:
elif (m[i]=='cookie' and m[i+1]!='milk'):
what if i+1 is out of range?
e.g.,
Consider the input to be:
1
3
cookie milk cookie
1 is the number of testcases.
3 is the number of elements in the
list.
'cookie milk cookie' composes the list elements.
When the value of i is 2 and your code's 14th line comes to execution, it will generate an index out of bound error(because m[i+1] i.e. m[3] doesn't exist!) which is a type of runtime error and thus generates NZEC error.
Add this code after the 7th line of your code:
if m[-1]=='cookie':
f.append(0)
continue
You could try using
try:
#working code
except Exception:
pass
Try and catch exception for java code.

Runtime Error (NZEC) - Python / Codechef

I'm getting started with Codechef. I submitted following code in python to solve this question.
The code below works fine with codechef's online (python) IDE as well as my local IDE. But, when I submit it on codechef, it results in Runtime Error (NZEC). Can someone explain to me the reason behind this?
withdrawCash = int(input())
totalCash = int(input())
if withdrawCash < totalCash and withdrawCash % 5 is 0:
totalCash = (totalCash - withdrawCash) - 0.5
print(totalCash)
The problem supplies both the inputs in a single line. Your code waits for input in 2 lines. Change it to:
withdrawCash,totalCash = map(int,raw_input.split())
NZEC (Non Zero Exit Code) occurs when your code doesn't return zero on exit. It can happen due to a number of reasons, but if splitting the input doesn't solve the problem, add an exception for EOFerror. Just write:
try:
withdrawCash = int(input().split()) # raw_input in the case of Python 2
except EOFerror:
print ("exit") # this is jst a pseudo statement `
Use indentation properly. I am currently using an android app of stack exchange in which writing code is not so easy. codechef is pretty poor when it comes to Python. Switch to any other lang for CP.
Try directly submitting the code without running it on Codechef ide because it with me also it showed the same but when I submitted directly I got submitted successfully. so Directly submit your code.

Categories

Resources