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  阅读:376  来源: 互联网

标签: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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

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

ICode9版权所有