• + 0 comments

    how to avoid recursion:

    ``cache=[]
    _build_dp=_build_dp
    def _build_dp(last, s, array, rest, x, n):
        for j in reverse(len(n):
            if len(rest)>n:
                return 0
            if n==2:
                if s!=x:
                    return 1
                else:
                    return 0  
        _count=0
        if (len(rest)==n):
            for i in rest:
                if i!=s:
                    _count+=_build_dp(s, i,  array, [ j for j in rest if j!=i ], x, n-1)
        else:
            for i in array:
                if i!=s:
                    _count+=_build_dp(s, i,  array, [ j for j in rest if j!=i ], x, n-1)
            return _count