Advanced Python Assessment
Test your mastery of advanced Python concepts including iterators, generators, decorators, context managers, closures, and more.
Write a class Countdown that acts as an iterator counting down from a given start value to 1 (inclusive).
Countdown(n) should accept a positive integer.n down to 1.StopIteration.Write a generator function fibonacci(n) that yields the first n Fibonacci numbers.
The sequence starts with 0 and 1: 0, 1, 1, 2, 3, 5, 8, ...
Write a decorator count_calls that tracks how many times a function has been called.
.call_count attribute that starts at 0..call_count by 1.Write a decorator factory retry(max_attempts) that retries a function up to max_attempts times if it raises an exception.
"Attempt {n} failed" for each failed attempt (1-indexed).The code below is supposed to create a generator pipeline that:
1. Generates numbers 1-10
2. Filters to keep only even numbers
3. Squares each number
But it produces wrong results. Find and fix the bug.
This decorator is supposed to double the return value of any function, but it doesn't work. Find and fix the bug.
This context manager is meant to track whether we are inside the managed block. It sets tracker.active to True on entry and False on exit. But the exit never runs. Find and fix the bug.
What does this code print? Think carefully about how closures capture variables.
What does this code print? Pay attention to the walrus operator (:=) and how it assigns and returns a value simultaneously.
What does this code print? Think about how starred unpacking works with nested structures.
Refactor this code to use a single generator expression passed directly to sum() instead of the explicit loop. The result should be the same: the sum of squares of even numbers from 1 to 20.
Your solution must:
sum() call with a generator expression insideRefactor the merge_configs function to use dictionary unpacking (** operator) instead of the manual loop. The function should merge defaults with overrides, where overrides take precedence.
Your refactored function body should be a single return statement using {**dict1, **dict2} syntax.