this is a simple piece of code which is suppose to read n numbers and suppose to print how many numbers out of these n numbers are divisible by k
n=int(raw_i
PUT TRY AND EXCEPT I DID THIS IN MY CODECHEF CODE AND IT'S WORKS
try:
n , k = map(int , input().split())
ans=0
while n > 0:
t=int(input())
if(t%k == 0):
ans = ans + 1
n = n - 1
print(ans)
except:
pass
For pyth-3.4
n,k=[int(x) for x in input().split()]
count=0
while n>0:
n-=1
tmp=int(input())
if tmp%k==0:
count+=1
print (count)
I suppose the codechef question is this one. You should take in account that the value of n and k are around 10^7, which could be a problem with your program.
Also, n and k are on the same line. You are using raw_input twice, so you are reading two lines. This can be solved by using:
n, k = raw_input().split(" ")
n = int(n)
k = int(k)
If that won't help, you could try looping over an xrange instead, or using a different algorithm.
You are getting NZEC error because when you enter space separated integers in python it is treated as a single string and not as two integers like in C,C++ and Java: In your case, This should work:
n,k=map(int,raw_input().split())
Also in future remember, to input an integer array separated by spaces in python is:
a=map(int,raw_input().split())
codechef would show you an NZEC when your code throws an exception. There are many possible reasons for the occurrence of this error, including but not limited to unsafe input handling, operating on non-existent/incorrect data, division by zero.
In your case, the problem can be solved as mentioned by Reshure. While handling other cases is going to be code specific, we can program in a way to avoid NZECs caused due to unsafe input handling. The idea is to read the input at once and tokenize it using whitespaces as delimiters.
import sys
tokenizedInput = sys.stdin.read().split() # Delimit input by whitespaces
# Now iterate over tokenizedInput.
In your case, this would be as follows:
n, k = map(int, tokenizedInput[:2])
ans = 0
for _ in xrange(2, n):
t = int(tokenizedInput[_])
if t%k == 0:
ans = ans + 1
print ans