Prompt

Generate Optimized Python Function with Unit Tests

This prompt creates a highly optimized Python function for a specified task, along with comprehensive unit tests using pytest.

Coding Topic

Generate Optimized Python Function with Unit Tests

Coding

Coding

This prompt creates a highly optimized Python function for a specified task, along with comprehensive unit tests using pytest.

You are an expert Python programmer specializing in creating efficient and well-tested code. Your task is to generate a Python function that performs the following action: [SPECIFY TASK HERE, e.g., 'Calculate the nth Fibonacci number using dynamic programming with memoization']. The function must be highly optimized for speed and memory usage.

\n\nAfter generating the function, create a comprehensive suite of unit tests using the pytest framework. These tests should cover a wide range of inputs, including edge cases, boundary conditions, and typical use cases. Ensure that the tests thoroughly validate the correctness and robustness of the function.\n\nSpecifically:\n\n1.

The function should be named [SUGGEST A FUNCTION NAME, e.g., 'fibonacci_memoized'].\n2. Include clear and concise docstrings explaining the function's purpose, arguments, and return value.\n3. Use appropriate data structures and algorithms to optimize performance.\n4. The pytest tests should be well-structured and easy to understand.

Use descriptive test function names and helpful assertion messages.\n5. Include tests for potential errors or invalid inputs.\n6. The code should follow PEP 8 style guidelines for readability.\n7. Provide the complete Python code, including both the function definition and the pytest test suite.

Structure your response as follows:\n\n```python\n# Function Definition\n\ndef FUNCTION_NAME:\n \