Complete the code to build a max heap from an unsorted array.
def build_max_heap(arr): n = len(arr) for i in range(n // 2 - 1, -1, -1): heapify(arr, n, [1])
The heapify function is called starting from the last non-leaf node, which is at index i, moving upwards to the root.
Complete the code to swap the root of the heap with the last element during heap sort.
def heap_sort(arr): n = len(arr) build_max_heap(arr) for i in range(n-1, 0, -1): arr[0], arr[[1]] = arr[[1]], arr[0] heapify(arr, i, 0)
During each iteration, the root element at index 0 is swapped with the element at index i, which moves the largest element to its correct sorted position.
Fix the error in the heapify function to correctly maintain the max heap property.
def heapify(arr, n, i): largest = i left = 2 * i + 1 right = 2 * i + 2 if left < n and arr[left] [1] arr[largest]: largest = left if right < n and arr[right] [1] arr[largest]: largest = right if largest != i: arr[i], arr[largest] = arr[largest], arr[i] heapify(arr, n, largest)
The heapify function compares child nodes with the current largest node. To maintain a max heap, it should check if the child is greater than the current largest.
Fill both blanks to create a dictionary comprehension that maps each element to its squared value if the element is even.
squares = {x: x[1]2 for x in range(1, 11) if x [2] 2 == 0}The operator ** is used to square the number, and % checks if the number is even by verifying the remainder is zero.
Fill all three blanks to create a dictionary comprehension that maps uppercase strings to their lengths if length is greater than 3.
result = [1]: [2] for s in words if len(s) [3] 3}
The key is the uppercase version of the string using s.upper(), the value is the length len(s), and the condition checks if length is greater than 3 using >.