Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 11 - Multidimensional Lists - Programming Exercises - Page 387: 11.16

Answer

code

Work Step by Step

# 11.16 (Sort a list of points on y-coordinates) Write the following function to sort a list # of points on their y-coordinates. Each point is a list of two values for x- and ycoordinates. # # Returns a new list of points sorted on the y-coordinates # def sort(points): # For example, the points [[4, 2], [1, 7], [4, 5], [1, 2], [1, 1], [4, 1]] will be sorted # to [[1, 1], [4, 1], [1, 2], [4, 2], [4, 5], [1, 7]]. Write a test program that displays # the sorted result for points [[4, 34], [1, 7.5], [4, 8.5], [1, -4.5], [1, 4.5], # [4, 6.6]] using print(list). # Returns a new list of points sorted on the y-coordinates def sort(points): # insertion sort for i in range(1, len(points)): current = points[i] j = i - 1 while j >= 0 and current[1] < points[j][1]: points[j + 1] = points[j] j -= 1 points[j + 1] = current points = [[4, 34], [1, 7.5], [4, 8.5], [1, -4.5], [1, 4.5], [4, 6.6]] sort(points) print(points)
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.