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 returns self.
  • __exit__ records the end time and stores elapsed seconds in self.elapsed.
  • Does not suppress exceptions.
with Timer() as t:
    time.sleep(0.1)
print(t.elapsed)  # ≈ 0.1

Implement 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.

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