Do you know there's an integrated python library calls bisect that is included in your installed python package?
Bisect is an useful module provides functions for binary search to find the index to insert a value into a sorted list/array.
Let's learn how to use it by example:
import bisect
def indexInsert(self, list, val):
return bisect.bisect_left(list, val)
import bisect imports the bisect module from the bisect library in Python.
indexInsert() is a function that search the index to insert a target value in a number list with a sorted manner.
list is the parameter that refers to the number list that we are going to search.
val is the target value that we want to insert in the list.
bisect module that we imported previously call bisect_left() function.
It returns the insertion point of the target val in list such that all numbers on the left of val are less that val.
Insertion point means where it would be if it were inserted in order.
Congratulations on completing the tutorial!
Persevere through any challenges that arise, and remember that with each small victory, you move one step closer to your ultimate success.
See you in Python (6)!
© 2023 Harmony Pang. All rights reserved.
Comments