Unit 4 Session 2 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
YYYY-MM-DD
format) and a description of the scene.YYYY-MM-DD
allows for correct chronological ordering using string comparison.YYYY-MM-DD
format, and each date is unique within the list.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use Python's built-in sorting functionality to sort the list of scene records by the date field in each tuple.
1) Use the `sorted()` function or the `sort()` method on the list to sort the `scene_records` by the date field.
a) Specify the sort key as the first element of each tuple (the date).
2) Return the sorted list.
**⚠️ Common Mistakes**
- Incorrectly assuming that dates need special handling for sorting when they can be sorted as strings due to the `YYYY-MM-DD` format.
- Overcomplicating the sort logic by attempting to manually implement a sorting algorithm when Python's built-in sorting is sufficient and optimized.
def organize_scene_data_by_date(scene_records):
# Sort the list of scene records by the date (first element of each tuple)
return sorted(scene_records, key=lambda record: record[0])
Example Usage:
scene_records = [
("2024-08-15", "Climax"),
("2024-08-10", "Introduction"),
("2024-08-20", "Resolution"),
("2024-08-12", "Rising Action")
]
print(organize_scene_data_by_date(scene_records))
# Output: [('2024-08-10', 'Introduction'), ('2024-08-12', 'Rising Action'), ('2024-08-15', 'Climax'), ('2024-08-20', 'Resolution')]
scene_records = [
("2023-07-05", "Opening"),
("2023-07-07", "Conflict"),
("2023-07-01", "Setup"),
("2023-07-10", "Climax")
]
print(organize_scene_data_by_date(scene_records))
# Output: [('2023-07-01', 'Setup'), ('2023-07-05', 'Opening'), ('2023-07-07', 'Conflict'), ('2023-07-10', 'Climax')]