Complete the code to start binary search with the correct initial left index.
def binary_search(arr, target): left = [1] right = len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1
The binary search starts with the left index at 0, which is the first element of the sorted list.
Complete the code to calculate the middle index correctly in binary search.
def binary_search(arr, target): left = 0 right = len(arr) - 1 while left <= right: mid = ([1] + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1
The middle index is the average of left and right indices, so we add left and right and divide by 2.
Fix the error in the binary search condition to correctly continue the search loop.
def binary_search(arr, target): left = 0 right = len(arr) - 1 while [1]: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1
The loop should continue while left is less than or equal to right to check all possible positions.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['tree', 'cat', 'house', 'dog'] lengths = {word: [1] for word in words if [2]
The dictionary maps each word to its length (len(word)) only if the length is greater than 3 (len(word) > 3).
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is less than or equal to 4.
words = ['tree', 'cat', 'house', 'dog'] result = { [1]: [2] for word in words if [3] }
The dictionary maps each word in uppercase (word.upper()) to its length (len(word)) only if the length is 4 or less (len(word) <= 4).
