NZEC in python on spoj for AP2
Question: I wrote the following two codes

##  FCTRL2.py

    
    
    import sys;
    def fact(x):
        res = 1
        for i in range (1,x+1):
            res=res*i
        return res;
    
    t = int(raw_input());
    for i in range (0,t):
        print fact(int(raw_input()));
    

and

## AP2.py

    
    
    import sys;
    
    t = int(raw_input());
    for i in range (0,t):
        x,y,z = map(int,sys.stdin.readline().split())
        n = (2*z)/(x+y)
        d = (y-x)/(n-5)
        a = x-(2*d)
        print n
        for j in range(0,n):
            sys.stdout.write(a+j*d)
            sys.stdout.write(' ')
        print' '
    

FCTRL2.py is accepted on spoj whereas AP2.py gives NZEC error. Both work fine
on my machine and i do not find much difference with regard to returning
values from both. Please explain what is the difference in both and how do i
avoid NZEC error for AP2.py


Answer: There may be extra white spaces in the input. A good problem setter would
ensure that the input satisfies the specified format. But since spoj allows
almost anyone to add problems, issues like this sometimes arise. One way to
mitigate white space issues is to read the input at once, and then tokenize
it.

    
    
    import sys;   # Why use ';'? It's so non-pythonic.
    
    inp = sys.stdin.read().split()    # Take whitespaces as delimiter
    t = int(inp[0])
    readAt = 1
    for i in range (0,t):
        x,y,z = map(int,inp[readAt:readAt+3])    # Read the next three elements
        n = (2*z)/(x+y)
        d = (y-x)/(n-5)
        a = x-(2*d)
        print n
        #for j in range(0,n):
        #    sys.stdout.write(a+j*d)
        #    sys.stdout.write(' ')
        #print ' '
        print ' '.join([str(a+ti*d) for ti in xrange(n)]) # More compact and faster
        readAt += 3   # Increment the index from which to start the next read
    

