Anyone,kindly help me out . I have tried a million times but cannot even get the first test “test” to pass and only 2 of the other test cases in “tests”! Where may I be wrong?
def count_rotations_binary(nums):
lo = 0
hi = len(nums)-1
while lo<=hi:
mid = (lo + hi)//2
mid_number = nums[mid]
# Uncomment the next line for logging the values and fixing errors.
print("lo:", lo, ", hi:", hi, ", mid:", mid, ", mid_number:", mid_number)
if mid >=lo and nums[mid-1]<nums[mid]:
# The middle position is the answer
return mid
elif nums[mid]<nums[hi]:
# Answer lies in the right half
#hi = mid - 1
lo = mid + 1
else:
# Answer lies in the left half
lo = mid + 1
#hi = mid - 1
return 0