Solutions for index of the element in the array

    • @JavierVow
    • @jacob, submitted in Python 2, achived 100% score
    import json,random
    def find_element(arr, element, start, end):
    
        if start == end:
            return
    
        mid = start + ((end - start) // 2)
    
        if arr[mid] == element:
            return mid
        elif arr[mid] > element:
            if arr[start] >= element:
                return find_element(arr, element, start, mid)
            else:
                return find_element(arr, element, mid, end)
        elif arr[mid] < element:
            if arr[start] <= element:
                return find_element(arr, element, start, mid)
            else:
                return find_element(arr, element, mid, end)
    
    
    def solve(arr, element):
        element_pos = find_element(arr, element, 0, len(arr))
        return element_pos
    • @JavierVow
    • @jacob, submitted in Python 3, achived 100% score
    import json,random
    def find_element(arr, element, start, end):
    
        if start == end:
            return
    
        mid = start + ((end - start) // 2)
    
        if arr[mid] == element:
            return mid
        elif arr[mid] > element:
            if arr[start] >= element:
                return find_element(arr, element, start, mid)
            else:
                return find_element(arr, element, mid, end)
        elif arr[mid] < element:
            if arr[start] <= element:
                return find_element(arr, element, start, mid)
            else:
                return find_element(arr, element, mid, end)
    
    
    def solve(arr, element):
        element_pos = find_element(arr, element, 0, len(arr))
        return element_pos