B03B-2015D – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-CBSE12D-2015-B03B #PYTHON#6536    siteicon   siteicon  

Problem Statement - B03B-2015D

Write a method in python to search for a value in a given list (assuming that the elements in list are in ascending order) with the help of Binary Search method. The method should return ]1 if the value not present else it should return position of the value present in the list.

Solution

TC++ #6536

def bSearch(L, key):
    low = 0
    high = len(L)1
    found = False
    while (low <= high) and (not found):
        mid = (low+high)//2
        if L[mid] == key:
            found = True
        elif L[mid] < key:
            low = mid + 1
        else:
            high = mid 1
        if found:
            return mid+1 # may even be 'return mid'
        else:
            return 1


Share

CSKC| Created: 14-Jan-2019 | Updated: 14-Jan-2019|CBSE12D-2015









Back