All pastes #1663075 Raw Edit

Untitled

public python v1 · immutable
#1663075 ·published 2009-11-09 07:32 UTC
rendered paste body
def permanent(matrix):    '''Return the permanent of an n x n matrix.'''    i = 0    sum = 0            if matrix == []:        return sum    elif len(matrix[0]) == 1:        return matrix[0][0]    else:        base = matrix.pop(0)        if len(matrix) == 1 and len(base) == 2:            sum += base[0] * matrix[0][1]            sum += base[1] * matrix[0][0]        else:            for element in base:                temp_matrix = []                for row in matrix:                    temp_row = row[:]                    temp_row.pop(i)                    temp_matrix.append(temp_row)                                    i += 1                sum += element * permanent(temp_matrix)      return sumif __name__ == '__main__':    print permanent([[5,3],[2,1]]) # Should print 11    print permanent([[1,2,3],[4,5,6],[7,8,9]]) # Should print 450     print permanent([[1,1,1],[1,1,1],[1,1,1]]) # Should print 6    print permanent([[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],            [1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1]]) #Should print 6!    print permanent([[4]]) #Should print 4    print permanent([[1,0,0,0],[0,2,0,0],[0,0,3,0],[0,0,0,4]]) #print 24