Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I edited your code, using string formatting and indentation to get a proper output. More about string formatting see here.

def plusminus(x):
    return '+' if x >= 0 else '-'

def calculator(L1,L2,d=0):  
    # d   -  recursion depth counter, used for indentation    
    c=0
    m=len(L1)
    n=len(L2)
    if m==0:
        pass  
    elif m==1:
        l1=L1[0]
        l2=L2[0]
        if l1<l2:
            c=c+l1            
        print d*'          '+'c{} = {} '.format(d,c)    
    else:
        for i in range(m):
            l1=L1[i]
            l2=L2[i]
            if l1<l2:
                c=c+l2
                print d*'          '+'c{} = {} '.format(d,c)
            else:
                L3 = L1[0:i] + L1[i+1:m]   # in this case list slicing is shorter and more readable 
                L4 = L2[0:i] + L2[i+1:n]
                print d*'          '+'c{} = {} {} calculator({},{})'.format(d,c,plusminus((-1)^m),L3,L4)
                c2 = calculator(L3,L4,d+1)
                c1 = c  # copy unchanged c for output
                c=c+(-1)^m*c2
                print d*'          '+'c{} = {} {} {} = {}'.format(d,c1,plusminus((-1)^m),c2,c)
                print 
    return c

#L1=[5,7,3,8]; L2=[7,5,2,6] 
L1=[5,7,3]; L2=[7,5,2]
#L1=[5,7]; L2=[7,5] 
#L1 = [5]; L2 = [7]
print 'calculator({},{})'.format(L1,L2)
calculator(L1,L2)