Python program to Search for Binary Numbers

from random import randint
def num_search(first,last):
 mid = len(first) //2
 mini = 0
 maxi = len(first) - 1
 while first[mid] != last and mini <= maxi:
    if last > first[mid]:
        mini = mid + 1
    else:
        maxi = mid -1
    mid = (mini + maxi) // 2
 if mini > maxi:
    return None
 else:
    return mid
x = []
for i in range(15):
 x.append(randint(1,20))
x.sort()
print(x)
num = int(input("Insert any number to search the list: "))
print(num,"is found at index",num_search(x,num))

 

 

 

Output:

[1, 3, 4, 5, 5, 7, 7, 8, 10, 12, 14, 14, 15, 18, 20]
Insert any number to search the list: 12
12 is found at index 9

 

[4, 7, 7, 7, 7, 8, 8, 10, 11, 12, 12, 13, 13, 16, 20]
Insert any number to search the list: 13
13 is found at index 11

 

 

Subscribe For Daily Updates