A Georgia Tech BuzzPort component stores record identifiers in ascending order. Given the sorted list and a target identifier, implement binary search to return the target's index, or -1 if it is absent.
Your algorithm must repeatedly compare the target with the middle element and discard the half that cannot contain the target. Do not use Python's built-in search or index methods.
Implement binary_search(nums, target), where nums is a sorted list of integers in nondecreasing order and target is an integer. Return any valid index containing target; if the target does not occur, return -1.
def binary_search(nums, target):