Retry Wrapper Built by Hand

Hard ⏱ 18 min 33% acceptance ★★★★☆ 4.3
Write a function with_retry(func, attempts) that returns a new function. When the returned function is called with some argument x, it should call func(x); if func raises a ValueError, it should retry up to attempts total tries. If all attempts raise ValueError, let the final ValueError propagate. If any attempt succeeds, return its result immediately without further retries.

Examples

Example 1
Input
calls = []
def flaky(x):
    calls.append(x)
    if len(calls) < 3:
        raise ValueError("fail")
    return x * 10
safe = with_retry(flaky, 5)
safe(7)
Output
70
Explanation

The first two calls raise ValueError and are retried; the third call (still within 5 attempts) succeeds and returns 7*10=70.

Constraints

  • attempts is a positive integer representing the total number of tries, not the number of retries after the first.
  • Only ValueError should be caught and retried; other exceptions propagate immediately.

Topics

Functionshigher-order functionsclosures

Companies

AmazonServiceNow

Hints

Hint 1

Use a for loop over range(attempts); inside, wrap the call in try/except ValueError.

Hint 2

On the last iteration, if it still fails, re-raise (let the loop exhaust and the exception propagate naturally by not catching it on the final attempt, or re-raise inside the except when it is the last attempt).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.