PYTHON CODING This function takes a list of points and then returns a new list of points, which starts with the first point that was given in the list, and then followed by points closest to the start point. Example: points = [a, b, c, d, e, f, g] point a is the strating point soe the new list must start with a. we then use the fistance function to calculate which points will be closest to a. lets say f is closest to a, therefore, second point in the new list will be f. Next we find which point is closest to f, lets say b is closest, therefore b is the next point in our new list, and so on. new list = [a, f, b, ......] ------- Here is the function that needs to be created: def solution_path(points) : # insert code here return path Please use any of the following function to develop the solution_path function Distance function - calculates the distance between two points def distance(p1, p2) : distance = sqrt (((p1 [0] - p2 [0]) **2) + ((p1 [1] - p2 [1]) **2)) return (distance) Find_closest function - calculates the closest point to the starting point def find_closest(start_point, remaining_points): closest_distance = 99999 for coordinate in remaining_points: dist = distance(start_point, coordinate) if(dist < closest_distance): closest_distance = dist closest_point = coordinate return closest_point Path_distance function - calculates the total distance between all points def path_distance(path) : total_distance = 0 for element in range(0, len(path) - 1): total_distance += distance(path[element], path[element + 1]) return total_distance Note: the function should return a new list, not modify the existing list. Hint: use a while loop to repeat while the list of remaining points is not empty Here is an exmaple of the input and output of the function: points = [(5, 8), (5.5,3), (3.5,1.5), (2.5,3.5), (7.5,9), (1,3), (7,9.5), (9,2), (1,0), (6,8), (3,7.5), (8,2.5)] path = solution_path(points) print(path) # the first point in the input list is (5, 8), so that appears first in our output list, # the closest point to our start point (5, 8) is (6, 8), so (6, 8) appears second in our list, # the closest remaining point to (6, 8) is (7.5, 9), so (7.5, 9) appears third in our list, # and so on. So the best path should be # [(5, 8), (6, 8), (7.5, 9), (7, 9.5), (3, 7.5), (2.5, 3.5), (1, 3), (3.5, 1.5), (5.5, 3), (8, 2.5), (9, 2), (1, 0)]
PYTHON CODING
This function takes a list of points and then returns a new list of points, which starts with the first point that was given in the list, and then followed by points closest to the start point.
Example:
points = [a, b, c, d, e, f, g]
point a is the strating point soe the new list must start with a. we then use the fistance function to calculate which points will be closest to a. lets say f is closest to a, therefore, second point in the new list will be f. Next we find which point is closest to f, lets say b is closest, therefore b is the next point in our new list, and so on.
new list = [a, f, b, ......]
-------
Here is the function that needs to be created:
def solution_path(points) :
# insert code here
return path
Please use any of the following function to develop the solution_path function
Distance function - calculates the distance between two points
def distance(p1, p2) :
distance = sqrt (((p1 [0] - p2 [0]) **2) + ((p1 [1] - p2 [1]) **2))
return (distance)
Find_closest function - calculates the closest point to the starting point
def find_closest(start_point, remaining_points):
closest_distance = 99999
for coordinate in remaining_points:
dist = distance(start_point, coordinate)
if(dist < closest_distance):
closest_distance = dist
closest_point = coordinate
return closest_point
Path_distance function - calculates the total distance between all points
def path_distance(path) :
total_distance = 0
for element in range(0, len(path) - 1):
total_distance += distance(path[element], path[element + 1])
return total_distance
Note: the function should return a new list, not modify the existing list.
Hint: use a while loop to repeat while the list of remaining points is not empty
Here is an exmaple of the input and output of the function:
points = [(5, 8), (5.5,3), (3.5,1.5), (2.5,3.5), (7.5,9), (1,3), (7,9.5), (9,2), (1,0), (6,8), (3,7.5), (8,2.5)]
path = solution_path(points)
print(path)
# the first point in the input list is (5, 8), so that appears first in our output list,
# the closest point to our start point (5, 8) is (6, 8), so (6, 8) appears second in our list,
# the closest remaining point to (6, 8) is (7.5, 9), so (7.5, 9) appears third in our list,
# and so on. So the best path should be
# [(5, 8), (6, 8), (7.5, 9), (7, 9.5), (3, 7.5), (2.5, 3.5), (1, 3), (3.5, 1.5), (5.5, 3), (8, 2.5), (9, 2), (1, 0)]
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images