ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

This function takes a list of points and then returns a new list of points, which starts with the fi

2022-04-22 12:02:24  阅读:144  来源: 互联网

标签:distance closest point list points path


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. 

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

 

(由留学作业帮www.homeworkhelp.cc整理编辑)
Bartleby|Bookrags|brainly.com|Coursehero|Chegg|eNotes|Ebook|gradebuddy|Grammerlly|Numerade |QuillBot|Oneclass|Studypool|SaveMyEaxms|Studymode|ScholarOne|SlideShare|SkillShare|Scribd|SolutionInn|Study.com|Studyblue

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)]

------

best_path(points) == [(2.8, 9.8), (4.4, 8.8), (5.0, 8.2), (6.8, 7.4), (3.4, 6.9), (0.7, 7.9), (0.3, 8.3), (4.8, 3.2), (4.1, 0.3), (4.5, 0.0), (7.6, 1.1), (7.7, 1.8)]
# Should return True

标签:distance,closest,point,list,points,path
来源: https://www.cnblogs.com/coursehero/p/16178265.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有