Implement a policy-driven executor that classifies failures, retries with backoff, and falls back to alternative tools.
Full problem description visible. Upgrade to unlock the editor, test cases, and solution.
Implement execute_with_policy(fn, args, is_retryable, fallbacks, max_attempts=3).
- Call fn(*args) inside a try/except block.
- On exception, call is_retryable(error). If False, re-raise immediately (terminal failure).
- If retryable, sleep 0.5 * (2 ** attempt) seconds, then retry (up to max_attempts total calls).
- After max_attempts failures, try each callable in fallbacks (a list) in order.
- If any fallback succeeds, return its result.
- If all fallbacks fail, re-raise the last captured exception.
- On success at any point, return the result immediately.
₹999/monthLimited period launch pricing.
Get the full problem statement, test cases, interactive editor, solution explanation, and visual diagram.
import time
def execute_with_policy(fn, args, is_retryable, fallbacks, max_attempts=3):
last_error = NoneRetry with backoff, then try fallbacks
raise last_error
Already have Pro access? Sign in