1st promptCold start — includes initialization
>Write a Python function to sort a list
Here's a Python function that sorts a list using quicksort:
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
# Example usage
numbers = [3, 6, 8, 10, 1, 2, 1]
print(quicksort(numbers)) # [1, 1, 2, 3, 6, 8, 10]
The algorithm picks a pivot, partitions the list into smaller, equal, and larger elements, then recursively sorts each partition and concatenates the results.
2nd promptWarm — model ready, no init overhead
>Now add type hints and a docstring
def quicksort(arr: list[int]) -> list[int]:
"""Sort a list using the quicksort algorithm.
Args:
arr: List of integers to sort.
Returns:
A new sorted list.
"""
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
Added type hints for the input and return type, plus a Google-style docstring.