I want to rewrite this code, so it won't use the range()
function. I've had a search around, but it only suggests using enumerate()
in its place. But I'd like for it to be done as a regular for
loop, even if it's a more difficult or complex way, so I can understand how and why the range()
function simplifies the code.
What I've written so far:
def distance(x1, x2) :
return math.sqrt( ((x2[0]-x1[0])**2) + ((x2[1]-x1[1])**2) )
def route(points):
total = 0
for element in range(1, len(points)):
total += distance(points[element - 1], points[element])
return total
route([(4, 2), (6, 3), (5, 1)])
I'm unsure whether the distance
function may also need to be altered to work through the list of tuples. But I essentially want to have the total distance between one point and the next as indexed in the list, as if something were travelling between the points.