# Author: Scott Wehrwein
# Date: 3/6/23
# An implementation of binary search


def find(v, lst):
    """ 1. Return the index of the first occurrence of v in lst. 
    Return -1 if v is not in the list. Precondition: lst is a list. """    
    for i, val in enumerate(lst):
        if val == v:
            return i
    return -1


def binary_search(v, sorted_lst):
    """ Return the index of the first occurrence of v in lst.  Return
    -1 if v is not in the list. Precondition: lst is a list of things that
    can be compared with the < operator, and is in sorted order 
    (i.e. lst[i] <= lst[i+1] for all i in range(len(lst)-1) """

    s = 0
    e = len(sorted_lst)
    
    while s < e:
        mid = (e+s) // 2
        if v == sorted_lst[mid]:
            return mid
        
        if v < sorted_lst[mid]:
            e = mid
        else: # v > sorted_lst[mid]
            s = mid+1

    return -1

# lst = [2, 2, 10]
# 
# for i, v in enumerate(lst):
#     print(i, v)
#     
    