Productivity applications often rely on a centralized "Command Engine" to process a stream of user actions. This engine is responsible for maintaining the state of a task list, ensuring that every addition, update, and deletion is executed in the precise order it was received. Your task is to build the backend logic for this processor, transforming a sequence of raw command tuples into a final, structured task registry.
Methodology
To maintain a consistent application state, the processor must follow a sequential execution pipeline that handles data mutation and boundary safety:
Step 1: State Initialization We initialize an empty linear data structure (List) to act as the primary storage for our task objects.
Step 2: Command Parsing For every command in the input sequence, we unpack the instruction into an Action (the operation type) and a Value (the target data or index).
Step 3: Operations & State Mutation The state is modified based on the instruction type:
- Addition: A new task object {"name": v, "done": False} is appended to the end of the list.
- Completion: The "done" attribute of a specific element is updated to
True. - Removal: A specific element is purged from the list, causing subsequent elements to shift indices.
Step 4: Boundary Validation Before attempting to update or delete, the engine must verify that the provided index () exists within the current bounds of the list to prevent runtime crashes.
Implementation
In Python, this logic is implemented using a List of Dictionaries. You must iterate through the commands and apply the changes to your task list in real-time. Use the .append() method for additions and the .pop() method for deletions. To ensure the application is robust, wrap your index-dependent operations in conditional checks that compare the input value against the current len() of your list.
🛠️ System Note: Standard Python list indices are zero-based. If an "ADD" command creates the first task, its index is 0.
Consider this command stream: [("ADD", "Task A"), ("DONE", 0)]
- ADD: List becomes
[{"name": "Task A", "done": False}]. - DONE: Index 0 is valid. Target task is updated to
True. - Result:
[{"name": "Task A", "done": True}]
Validation
To ensure your solution passes our automated verification system (==), you must satisfy the following:
- Format: Return a list where every element is a dictionary with keys
"name"(string) and"done"(boolean). - Safety: If a
DONEorDELETEcommand targets an invalid index, your code must do nothing and proceed to the next command.
The Challenge
Implement the function process_tasks(commands). It must accept a list of tuples representing user actions and return the final list of tasks.
def process_tasks(commands): # Step 1: Initialize empty task list # Step 2: Loop through commands and unpack (action, value) # Step 3: Implement ADD, DONE, and DELETE logic # Step 4: Include index safety checks # Final Step: Return the task list pass
