Example input
[LANGUAGE]: Python
[COMMENTS_YN]: Yes
[FUNCTIONALITY]: Sort a list of integers in ascending order
[CONSTRAINTS]: Handle empty lists and lists with negative numbers
[LIBRARY_FRAMEWORK]: Standard Python libraries only
[INPUT_OUTPUT]: Input: A list of integers, Output: Sorted list of integers
[TESTCASES_YN]: Yes
Example output
USER:
"Your primary task is to generate functional code snippets based on user specifications. Here’s what’s expected to ensure the code is of excellent quality:
1. Accuracy: The code should accomplish exactly what the user describes, taking into account all specifics or constraints they provide.
2. Efficiency: Ensure the code is optimized. Avoid redundant or unnecessary lines. Think about the best data structures or algorithms that fit the task.
3. Readability: Your code should be easy for humans to understand. Use clear variable and function names. Avoid “clever” solutions if a straightforward one will do.
4. Well-commented: Always provide essential comments, especially if the code contains nuances that might not be immediately obvious. However, respect the user’s preference if they specify their desired comment level.
5. Robustness: Think about potential edge cases and try to account for them. This ensures the code doesn’t just work—it works reliably.
6. Maintainability: Structure the code in a way that it can be easily modified or expanded in the future.
INPUTS:
• Programming Language:
• Python
• Desired Functionality:
• Sort a list of integers in ascending order
• Input & Output:
• Input: A list of integers, Output: Sorted list of integers
• Use of Libraries or Frameworks:
• Standard Python libraries only
• Code Comments:
• Yes
• Additional Constraints:
• Handle empty lists and lists with negative numbers
• Include Test Cases?:
• Yes
Instructions to ChatGPT:
• Use bold for function or method names, and italicize any particularly important notes or cautions.
• Provide only the requested code. If further clarification on user inputs is needed or if there are concerns about the feasibility, communicate directly. Otherwise, remain concise and refrain from unnecessary commentary unless prompted by the user."
ChatGPT:
def **sort_integers**(numbers):
"""
Sorts a list of integers in ascending order.
Args:
numbers (list): A list of integers.
Returns:
list: Sorted list of integers in ascending order.
Notes:
- Handles empty lists and lists with negative numbers.
"""
# Edge case: Return the list as is if it is empty
if not numbers:
return numbers
# Using Python's built-in sorted() function for simplicity and efficiency
sorted_numbers = sorted(numbers)
return sorted_numbers
# Test cases to validate the functionality
if __name__ == "__main__":
test_cases = [
{"input": [], "expected": []}, # Empty list
{"input": [3, 1, 2], "expected": [1, 2, 3]}, # Simple case
{"input": [-1, -5, 0, 4, 2], "expected": [-5, -1, 0, 2, 4]}, # List with negative numbers
{"input": [10, 10, 10], "expected": [10, 10, 10]}, # List with repeated numbers
]
for idx, case in enumerate(test_cases):
result = **sort_integers**(case["input"])
assert result == case["expected"], f"Test case {idx + 1} failed: Expected {case['expected']}, got {result}"
print("All test cases passed!")