Codepath

Organize Scene Data by Date

Unit 4 Session 2 Advanced (Click for link to problem statements)

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What is the goal of the problem?
    • A: The goal is to sort a list of scene records by date and return the sorted list.
  • Q: What are the inputs?
    • A: The input is a list of tuples, where each tuple contains a date (in YYYY-MM-DD format) and a description of the scene.
  • Q: What are the outputs?
    • A: The output is a list of tuples sorted by the date in ascending order.
  • Q: How should dates be compared?
    • A: Dates should be compared lexicographically (as strings) since the format YYYY-MM-DD allows for correct chronological ordering using string comparison.
  • Q: Are there any assumptions about the input?
    • A: The input list contains valid date strings in the YYYY-MM-DD format, and each date is unique within the list.

P-lan

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.

I-mplement

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')]
Fork me on GitHub