First time here? Check out the FAQ!

Ask Your Question
1

A (beginner) problem with Cython, II

asked 11 years ago

Why the following code generates a problem in Sage-Cython ?
(note the recursion and the global variable bb)

cpdef lintsolve(list T, list A, int m, int ep):
    global bb;
    cdef int t0,t,n,i,m1
    cdef list A1,T1
    if not A:
        bb=[]
    t0=T[0]
    t=len(T)
    if t==1 and m%t0==0:
        A1=[ep]
        A1.extend(A)
        A1.append(m//t0)
        bb.extend([A1])
    if t>1:
        n=m//t0
        for i in range(n+1):
            T1=T[1:]
            m1=m-i*t0
            A1=A[:]
                A1.append(i)
            lintsolve(T1,A1,m1,ep)
    return bb

while the following code runs very well in Sage-Python :

def lintsolve(T,A,m,ep):
    global bb;
    if not A:
        bb=[]
    t0=T[0]
    t=len(T)
    if t==1 and m%t0==0:
        A1=[ep]
        A1.extend(A)
        A1.append(m//t0)
        bb.extend([A1])
    if t>1:
        n=m//t0
        for i in range(n+1):
            T1=T[1:]
            m1=m-i*t0
            A1=A[:]
                A1.append(i)
            lintsolve(T1,A1,m1,ep)
    return bb
Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 11 years ago

Luca gravatar image

Neither work for me, but if I correct the indentation error at

        A1=A[:]
            A1.append(i)

both work for me.

Preview: (hide)
link

Comments

Thanks @Luca, but this is quite strange, this indentation error does not appear in my text, it appears only when I copy-past on the post, after pushing on the button "preformatted text"...!?

Sébastien Palcoux gravatar imageSébastien Palcoux ( 11 years ago )

Try by yourself, if you push on "edit", select the first code, and push on "preformatted text", then the first line and the line with "A1.append(i)" shift, whereas the other lines not... do you know why ?

Sébastien Palcoux gravatar imageSébastien Palcoux ( 11 years ago )

Anyway, by removing the indentation of my code and indenting again (the same), it works miraculously...

Sébastien Palcoux gravatar imageSébastien Palcoux ( 11 years ago )

I can't edit your question. The problem is maybe due to a mixture of tabs and spaces. What editor do you use?

Luca gravatar imageLuca ( 11 years ago )

Yes, it seems to be caused such a mixture, is it a bug ? I use gedit on Ubuntu.

Sébastien Palcoux gravatar imageSébastien Palcoux ( 11 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 11 years ago

Seen: 382 times

Last updated: Sep 05 '13