Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Refactoring the method.

$
0
0
I am solving the Travelling Salesman Problem. I have a cost matrix and i want to find the cost of a tour.
Method i have written works but my question is whether it is written in a Pythonic style. Any suggestions to improve the style are welcome.

def findTourCost(self, tour):
        total = 0
        for index, value in enumerate(tour):
            if index == len(tour) - 1:
                total = total + self.costMatrix[value][tour[0]]
                break
            
            total = total + self.costMatrix[value][tour[index + 1]]
    
        return total


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>