Reputation: 359
If I have a list
L = [
'AXX',
'XXX',
'XXG'
]
Suppose it is known that the 'distance' between an A
and an adjacent X
is 1 and the 'distance' between an A
and a diagonally located X
is 2. How would I be able to translate that into python?
Thanks
Upvotes: 0
Views: 239
Reputation: 106598
By your definition the distance between two cells in a matrix is quite simply their row difference plus their column difference , so all you need is a function that takes the position of the reference cell and the position of the other cell and does the said calculation:
def distance(row1, column1, row2, column2):
return abs(row2 - row1) + abs(column2 - column1)
so that:
distance(0, 0, 1, 1) # distance between A and the diagonally located X
would be 2
.
Upvotes: 2