views:

85

answers:

2

I always get a runtime error using python for submission on codechef. Can some one please help. Tried answering other questions too.. same error Works fine on my comp though!(I use python 2.6.5 on my comp. Answer is checked with python 2.5) This is an easy level question where i get Runtime error http://www.codechef.com/problems/FCTRL/

My code

import sys
def factorial_zeros(f):
    i=0
    j=0
    for x in range (1,f+1):
        if x%10 == 0:
            while x%10 == 0:
                x= x//10
                i +=1
            while x%5 == 0:
                x= x//5
                j +=1
        elif x%5 == 0:
            while x%5 == 0:
                x= x//5
                j +=1
    l = i+j
    return l


l=[]
i=int(raw_input())
for x in range(i):
    f = int(raw_input())
    f= factorial_zeros(f)
    if x == i-1:
        sys.stdout.write(str(f))
    else:
        print f
+1  A: 

I don't compete/submit at Codechef, but AFAIK it uses Python 2.5 rather than 2.6. Perhaps you are using something that is 2.5-specific? (although I can't find anything that is).

Edit:

It looks to me now that the problem isn't with Python versions at all. Notice in the problem statement that the input value N can be as large as 10^9. Trying the range(1,f+1) with such a large value of f will cause the interpreter to try to build a list with 10^9 elements. This will clearly exceed the memory limits for this problem on the judge machine, thus causing an uncaught exception that shows up as an RTE to you.

FWIW, your approach to solving the problem is wrong. even if you replaced range with xrange to avoid memory limits, you will still end up trying 10^9 iterations which will make your solution time out.

MAK
`i,j = 0,0` should be valid syntax on Python 2.5 as well.
lunaryorn
thanks but still getting runtime error
san8055
i tried with i=0j=0
san8055
still a runtime error
san8055
@san8055: There might be a few more 2.6-specific elements in your code still, but I don't see any. Maybe the input also contains redundant whitespace, which causes an exception when `int` is called. Also, your code looks weird and quite unpythonic. For example, why do you use both `print` and `sys.stdout.write`? Why not use one or the other?
MAK
@MAk the last output in the result must have no newline or space at the end, which forces me to use sys.stdout.write
san8055
@san8055: Why not simply add a `,` at the end? i.e. use `print str(f),`. Also, I don't think explicitly using `//` is necessary since all operands are integers anyway.
MAK
@MAK print str(f), leaves a space and u r right abt // :)
san8055
@MAK: Sorry to disagree, but you *are* mistaken. `i, j = 0, 0` *is* valid Python 2.5 code. It is nothing more than standard tuple packing in combination with standard tuple unpacking, both of which have been there for ages. Examples of these features can already be found in the tutorial of the Python 2.0(!) documentation, Section 5.3, Tuples and Sequences (http://docs.python.org/release/2.0/tut/node7.html#SECTION007300000000000000000)
lunaryorn
@lunaryorn: Yes, looks like I am mistaken indeed. Thanks for pointing that out. I was under the impression that you have to explicitly use tuples for the assignment. Correcting my answer now.
MAK
+1  A: 

The thing runs OK both under 2.6 and 2.5, and 2.4 for that matter. i, j = 0, 0 is fine since very ancient Python versions, like 1.5. The program even reads input and computes the results correctly, if slowly. Though I'd try sys.stdin.readline().strip() instead of raw_input().

Unless you specify which RuntimeError you're getting, complete with stacktrace preferably, hardly anyone can help you, all telepaths are on vacation.

9000
I get Runtime error NZEC
san8055
from FAQ-->Why do I get an NZEC?NZEC stands for Non Zero Exit Code. For C users, this will be generated if your main method does not have a return 0; statement. Other languages like Java/C++ could generate this error if they throw an exception.
san8055