BestPeers stores candidate record IDs in ascending order for fast lookup. Given a sorted list of integers and a target value, implement binary search using exactly one loop and return the target's index, or -1 if it is absent.
Your implementation must use an iterative approach with a single while or for loop. Do not call a library search function, use recursion, or create another loop through a helper function.
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 i such that nums[i] == target. If the target does not occur, return -1.
def binary_search(nums, target):