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 i
th item but Python does not!
Index
BisectPy.bisect_left
BisectPy.bisect_right
BisectPy.find_ge
BisectPy.find_gt
BisectPy.find_le
BisectPy.find_lt
BisectPy.index
BisectPy.insort_left
BisectPy.insort_right
BisectPy.bisect_left
— Functionbisect_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
BisectPy.bisect_right
— Functionbisect_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
BisectPy.find_ge
— Methodfind_ge(a, x)
Find leftmost item greater than or equal to x
in a
.
BisectPy.find_gt
— Methodfind_gt(a, x)
Find leftmost value greater than x
in a
.
BisectPy.find_le
— Methodfind_le(a, x)
Find rightmost value less than or equal to x
in a
.
BisectPy.find_lt
— Methodfind_lt(a, x)
Find rightmost value less than x
in a
.
BisectPy.index
— Methodindex(a, x)
Locate the leftmost value exactly equal to x
in a
.
BisectPy.insort_left
— Functioninsort_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.
BisectPy.insort_right
— Functioninsort_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.