BisectPy

This is a package that migrates Python's bisect module to Jula.

Note that since Julia's array index starts from 1 but Python starts from 0, the returned index of either bisect_left or bisect_right is always their Python's correspondence plus 1!

Also, the behavior of Python's a[:i] where a is an array is also different from Julia: Julia array includes the ith item but Python does not!

Index

BisectPy.bisect_leftFunction
bisect_left(a, x, lo = 1, hi = length(a) + 1)

Return the index where to insert item x in array a, assuming a is in an non-decreasing order.

The return value i is such that all e in a[:(i - 1)] have e < x, and all e in a[i:] have e >= x. So if x already appears in the array, insert!(a, i, x) will insert just before the leftmost x already there.

Arguments

Optional args lo (default 1) and hi (default length(a) + 1) bound the slice of a to be searched.

Examples

julia> bisect_left([1, 2, 3, 4, 5], 3.5)
4

julia> bisect_left([1, 2, 3, 4, 5], 2)
2

julia> bisect_left([1, 2, 3, 3, 3, 5], 3)
3
source
BisectPy.bisect_rightFunction
bisect_right(a, x, lo = 1, hi = length(a) + 1)

Return the index where to insert item x in array a, assuming a is in an non-decreasing order.

The return value i is such that all e in a[:(i - 1)] have e <= x, and all e in a[i:] have e > x. So if x already appears in the array, insert!(a, i, x) will insert just after the rightmost x already there.

Arguments

Optional args lo (default 1) and hi (default length(a) + 1) bound the slice of a to be searched.

Examples

julia> bisect_right([1, 2, 3, 4, 5], 3.5)
4

julia> bisect_right([1, 2, 3, 4, 5], 2)
3

julia> bisect_right([1, 2, 3, 3, 3, 5], 3)
6
source
BisectPy.insort_leftFunction
insort_left(a, x, lo = 1, hi = nothing)

Insert item x in array a, and keep it sorted assuming a is sorted.

If x is already in a, insert it to the left of the leftmost x. Optional args lo (default 1) and hi (default length(a)) bound the slice of a to be searched.

source
BisectPy.insort_rightFunction
insort_right(a, x, lo = 1, hi = nothing)

Insert item x in array a, and keep it sorted assuming a is sorted.

If x is already in a, insert it to the right of the rightmost x. Optional args lo (default 1) and hi (default length(a)) bound the slice of a to be searched.

source