Please generate the solution for the following task: { "Title":…
- User
Assistant: User: Please generate the solution for the following task: { "Title": "Advanced Merge Sort with Custom Comparator", "Task Description": "Implement a merge sort algorithm that allows for a custom comparator function to determine the order of sorting. The comparator should be able to handle both ascending and descending orders based on a flag parameter.", "Function Signature": "def advanced_merge_sort(arr: list, comparator: callable, descending: bool) -> list:", "Solve": "def merge(left, right, comparator, descending):\n result = []\n while left and right:\n if (comparator(left[0], right[0]) < 0) ^ descending:\n result.append(left.pop(0))\n else:\n result.append(right.pop(0))\n result.extend(left or right)\n return result\n\n\ndef merge_sort(arr, comparator, descending):\n if len(arr) <= 1:\n return arr\n mid = len(arr) // 2\n left = merge_sort(arr[:mid], comparator, descending)\n right = merge_sort(arr[mid:], comparator, descending)\n return merge(left, right, comparator, descending)\n\n\ndef advanced_merge_sort(arr: list, comparator: callable, descending: bool) -> list:\n return merge_sort(arr, comparator, descending)", "Input": "A list of integers 'arr' to be sorted, a comparator function 'comparator' that takes two integers and returns a negative, zero, or positive value, and a boolean 'descending' that indicates the sorting order.", "Output": "A new list containing the elements of 'arr' sorted according to the custom comparator and the specified order.", "Examples": [ { "input": "[5, 3, 8, 1, 2], lambda x, y: x - y, False", "output": "[1, 2, 3, 5, 8]" }, { "input": "[10, 20, 15, 30], lambda x, y: y - x, True", "output": "[30, 20, 15, 10]" } ] } Ensure that:
- The solution must not use any imports in the code (i.e., no import statements).
- The 'Solve' section should contain the solution matching the function signature.
- Answer format must be JSON and match the original structure. Assistant:
- Assistantjson