Advanced Task Scheduler: Build a Priority-based Task Manager in Python
Create a Python mini-project that manages a list of tasks with start times, durations, and priorities, optimizing scheduling to avoid conflicts based on priority and timing.
Challenge prompt
You are tasked with creating an advanced task scheduler that manages tasks with start times, durations, and priorities. Build a Python function named schedule_tasks(tasks) where tasks is a list of dictionaries, each dictionary containing 'id' (unique int), 'start' (ISO 8601 datetime string), 'duration' (minutes, int), and 'priority' (int, higher means more important). The function must return a schedule list of task ids sorted by actual execution order, ensuring that tasks do not overlap in execution timelines. If tasks overlap, the task with lower priority should be delayed until the conflicting task is finished. Tasks must run sequentially with no gaps unless forced by conflicts. Adjust start times accordingly to resolve overlaps based on priority. Your final output must be a list of task ids in the order they will execute, respecting all constraints.
Guidance
- • Parse and convert ISO 8601 datetime strings to Python datetime objects for accurate time calculations.
- • Sort initial tasks by start time and priority to manage execution order and conflict resolution.
- • Iteratively adjust conflicting tasks' start times so no two tasks overlap, always delaying lower-priority tasks.
- • Return only the ordered list of task ids reflecting the final execution order after resolving all overlaps.
Hints
- • Use the datetime module to handle time calculations and comparisons.
- • Think about simulating the schedule timeline, updating start times as you iterate through tasks.
- • Consider a greedy approach: always schedule the highest priority task available at the earliest possible time.
Starter code
from datetime import datetime, timedelta
def schedule_tasks(tasks):
# Your code here
pass
# Example task format
# tasks = [
# {'id': 1, 'start': '2024-06-01T09:00:00', 'duration': 60, 'priority': 2},
# {'id': 2, 'start': '2024-06-01T09:30:00', 'duration': 45, 'priority': 3},
# {'id': 3, 'start': '2024-06-01T10:00:00', 'duration': 30, 'priority': 1}
# ]Expected output
[2, 1, 3]
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.