Bisect Algorithm Functions in Python

1. bisect(list, num, beg, end) :- This function returns the position in the sorted list, where the number passed in argument can be placed so as to maintain the resultant list in sorted order. If the element is already present in the list, the right most position where element has to be inserted is returned. This function takes 4 arguments, list which has to be worked with, number to insert, starting position in list to consider, ending position which has to be considered.

2. bisect_left(list, num, beg, end) :- This function returns the position in the sorted list, where the number passed in argument can be placed so as to maintain the resultant list in sorted order. If the element is already present in the list, the left most position where element has to be inserted is returned. This function takes 4 arguments, list which has to be worked with, number to insert, starting position in list to consider, ending position which has to be considered.

3. bisect_right(list, num, beg, end) :- This function works similar to the “bisect()” and mentioned above.

# Python code to demonstrate the working of 
# bisect(), bisect_left() and bisect_right() 

# importing "bisect" for bisection operations 
import bisect 

# initializing list 
li = [1, 3, 4, 4, 4, 6, 7] 

# using bisect() to find index to insert new element 
# returns 5 ( right most possible index ) 
print ("The rightmost index to insert, so list remains sorted is : ", end="") 
print (bisect.bisect(li, 4)) 

# using bisect_left() to find index to insert new element 
# returns 2 ( left most possible index ) 
print ("The leftmost index to insert, so list remains sorted is : ", end="") 
print (bisect.bisect_left(li, 4)) 

# using bisect_right() to find index to insert new element 
# returns 4 ( right most possible index ) 
print ("The rightmost index to insert, so list remains sorted is : ", end="") 
print (bisect.bisect_right(li, 4, 0, 4)) 

4. insort(list, num, beg, end) :- This function returns the sorted list after inserting number in appropriate position, if the element is already present in the list, the element is inserted at the rightmost possible position. This function takes 4 arguments, list which has to be worked with, number to insert, starting position in list to consider, ending position which has to be considered.

5. insort_left(list, num, beg, end) :- This function returns the sorted list after inserting number in appropriate position, if the element is already present in the list, the element is inserted at the leftmost possible position. This function takes 4 arguments, list which has to be worked with, number to insert, starting position in list to consider, ending position which has to be considered.

6. insort_right(list, num, beg, end) :- This function works similar to the “insort()” as mentioned above.

# Python code to demonstrate the working of 
# insort(), insort_left() and insort_right() 

# importing "bisect" for bisection operations 
import bisect 

# initializing list 
li1 = [1, 3, 4, 4, 4, 6, 7] 

# initializing list 
li2 = [1, 3, 4, 4, 4, 6, 7] 

# initializing list 
li3 = [1, 3, 4, 4, 4, 6, 7] 

# using insort() to insert 5 at appropriate position 
# inserts at 6th position 
bisect.insort(li1, 5) 

print ("The list after inserting new element using insort() is : ") 
for i in range(0, 7): 
	print(li1[i], end=" ") 

# using insort_left() to insert 5 at appropriate position 
# inserts at 6th position 
bisect.insort_left(li2, 5) 

print("\r") 

print ("The list after inserting new element using insort_left() is : ") 
for i in range(0, 7): 
	print(li2[i], end=" ") 

print("\r") 

# using insort_right() to insert 5 at appropriate position 
# inserts at 5th position 
bisect.insort_right(li3, 5, 0, 4) 

print ("The list after inserting new element using insort_right() is : ") 
for i in range(0, 7): 
	print(li3[i], end=" ") 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值