Using Insertion Sort, Write a python function sort_rectangles that takes rectangle_records and record_title as a parameter and sort the rectangle_records in ascending order by record_title. You need to return the updated rectangle_records list.
We have the following rectangle records in a list of dictionaries.
Test Cases:
rectangle_records = [{"ID": "Rect1", "Length": 40, "Breadth": 25, "Color": "red"}, {"ID": "Rect2", "Length": 30, "Breadth": 20, "Color": "blue"}, {"ID": "Rect3", "Length": 70, "Breadth": 45, "Color": "green"}, {"ID": "Rect4", "Length": 20, "Breadth": 10, "Color": "purple"}]
Using Insertion Sort, Write a python function sort_rectangles that takes rectangle_records and record_title as a parameter and sort the rectangle_records in ascending order by record_title. You need to return the updated rectangle_records list.
NOTE: The type of record_title input can be ID, Length, Breadth, or Color.
>>> sort_rectangles(rectangle_records, "ID")
[{"ID": "Rect1", "Length": 40, "Breadth": 25, "Color": "red"}, {"ID": "Rect2", "Length": 30, "Breadth": 20, "Color": "blue"}, {"ID": "Rect3", "Length": 70, "Breadth": 45, "Color": "green"}, {"ID": "Rect4", "Length": 20, "Breadth": 10, "Color": "purple"}]
>>> sort_rectangles(rectangle_records, "Length")
[{'ID': 'Rect4', 'Length': 20, 'Breadth': 10, 'Color': 'purple'}, {'ID': 'Rect2', 'Length': 30, 'Breadth': 20, 'Color': 'blue'}, {'ID': 'Rect1', 'Length': 40, 'Breadth': 25, 'Color': 'red'}, {'ID': 'Rect3', 'Length': 70, 'Breadth': 45, 'Color': 'green'}]
>>> sort_rectangles(rectangle_records, "Breadth")
[{'ID': 'Rect4', 'Length': 20, 'Breadth': 10, 'Color': 'purple'}, {'ID': 'Rect2', 'Length': 30, 'Breadth': 20, 'Color': 'blue'}, {'ID': 'Rect1', 'Length': 40, 'Breadth': 25, 'Color': 'red'}, {'ID': 'Rect3', 'Length': 70, 'Breadth': 45, 'Color': 'green'}]
>>> sort_rectangles(rectangle_records, "Color")
[{'ID': 'Rect2', 'Length': 30, 'Breadth': 20, 'Color': 'blue'}, {'ID': 'Rect3', 'Length': 70, 'Breadth': 45, 'Color': 'green'}, {'ID': 'Rect4', 'Length': 20, 'Breadth': 10, 'Color': 'purple'}, {'ID': 'Rect1', 'Length': 40, 'Breadth': 25, 'Color': 'red'}]
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images