HardPro challengePython
Context Manager — Timer
PythonMetaprogrammingContext Managers
Implement a Timer context manager class with __enter__ and __exit__ that measures elapsed time.
__enter__records the start time and returnsself.__exit__records the end time and stores elapsed seconds inself.elapsed.- Does not suppress exceptions.
with Timer() as t:
time.sleep(0.1)
print(t.elapsed) # ≈ 0.1Implement solve() that:
1. Uses Timer to measure a time.sleep(0.05) block.
2. Returns True if elapsed >= 0.04 (accounting for system variance), False otherwise.
The checker also tests Timer directly with a controlled clock: returning
the same instance from __enter__, measuring elapsed time on normal exit,
and recording elapsed time while propagating the original exception. Usetime.perf_counter() to read the clock.
Sample tests
Test #1Elapsed >= 0.04 after 50ms sleep
Input: []
Output: true
Test #2Timer records non-zero elapsed time
Input: []
Output: true
Test #3Context manager returns elapsed on exit
Input: []
Output: true