I'm trying to make a function that will calculate all distances of each route. For example: A is at (0,0), B is at (1,0), C is at (0,1). Then there are 6 route to go through all towns like ABC or ACB or BAC or BCA or CAB or CBA. Here, my question is, how can I enumerate all patterns of factorial like ABC,ACB,BAC,BCA,CAB,CBA. It would be easy if it's 2! or 3!, but if for example 8!, it would be super hard. If you know, I want to know your advice!
Python has a bultin function itertools.permutations
. You can use it like so
from itertools import permutations
data = ['a', 'b', 'c']
print(list(permutations(data)))
Output:
[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
You can make data contain tuples like data = [(0, 0), (1, 0), (0, 1)]
Then the output will look like this
[((0, 0), (1, 0), (0, 1)), ((0, 0), (0, 1), (1, 0)), ((1, 0), (0, 0), (0, 1)), ((1, 0), (0, 1), (0, 0)), ((0, 1), (0, 0), (1, 0)), ((0, 1), (1, 0), (0, 0))]