Learn practical skills, build real-world projects, and advance your career

assignment-1-binary-search-practice

Use the "Run" button to execute the code.

!pip install jovian --upgrade --quiet
import jovian
# Execute this to save new versions of the notebook
jovian.commit(project="assignment-1-binary-search-practice")
[jovian] Attempting to save notebook.. [jovian] Updating notebook "abidcsedu/assignment-1-binary-search-practice" on https://jovian.ai [jovian] Uploading notebook.. [jovian] Uploading additional files... [jovian] Committed successfully! https://jovian.ai/abidcsedu/assignment-1-binary-search-practice
def count_rotations(nums):
    
    lo, hi = 0, len(nums) - 1
    first = nums[0]
    
    while lo <= hi:
        mid = (lo + hi)//2
        mid_number = nums[mid]
        
        if mid_number < first:
            if nums[mid - 1] > mid_number:
                return mid
            else:
                hi = mid - 1
        else:
            lo = mid + 1
    return 0