Search in Rotated Sorted Array in Python

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        """
        Search for the target in a rotated sorted array and return its index, or -1 if not found.

        :param nums: List[int], a rotated sorted array
        :param target: int, the target value to search for
        :return: int, the index of the target in nums, or -1 if not found
        """
        # Initialize the left and right pointers to the start and end of the array.
        left, right = 0, len(nums) - 1

        # Begin the binary search loop.
        while left <= right:
            # Calculate the mid index to divide the array into two halves.
            mid = (left + right) // 2

            # If the middle element is the target, return its index.
            if nums[mid] == target:
                return mid

            # Determine which part of the array is sorted.
            # Check if the left half is sorted.
            if nums[left] <= nums[mid]:
                # Check if the target is within the range of the sorted left half.
                if nums[left] <= target < nums[mid]:
                    # If the target is in the left half, discard the right half.
                    right = mid - 1
                else:
                    # If the target is not in the left half, discard the left half.
                    left = mid + 1
            else:
                # The right half is sorted.
                # Check if the target is within the range of the sorted right half.
                if nums[mid] < target <= nums[right]:
                    # If the target is in the right half, discard the left half.
                    left = mid + 1
                else:
                    # If the target is not in the right half, discard the right half.
                    right = mid - 1

        # If the target is not found, return -1.
        return -1

Next