Generate Optimized Python Function with Unit Tests

Apr 29, 2025

Description

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

Prompt

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  \

How to Use

1

Copy the prompt

Click the copy button above to copy the entire prompt to your clipboard

2

Open AI model

Go to your preferred AI model or any compatible AI assistant

3

Paste & customize

Paste the prompt into the chat and modify it for your specific needs

4

Send & enjoy

Send the prompt and wait for the AI to generate a response

Comments (0)

No comments yet

Be the first to share your thoughts!