[python] [c ++] bisect (* _bound) in reverse (descending) list

Since bisect does not support descending list, I made it. https://codeday.me/jp/qa/20190215/252510.html There are some, but I wonder if it is possible to implement it by myself.

from bisect import*

cargo=[1,4,6,43,7,3,6,3,7,32]
length=len(cargo)
cargo_ascending =[1,3,3,4,6,6,7,7,32,43]
cargo_descending=[43,32,7,7,6,6,4,3,3,1]

#cargo_It's not necessary to take the descending argument, but for the time being
def bisect_reverse_right(cargo_descending,x,lb=0,ub=length):  
    return length-bisect_left(cargo_ascending,x,length-ub,length-lb)

def bisect_reverse_left(cargo_descending,x,lb=0,ub=length):
    return length-bisect_right(cargo_ascending,x,length-ub,length-lb)

print(bisect_left(cargo_ascending,7))  #6
print(bisect_right(cargo_ascending,7))  #8
print(bisect_left(cargo_ascending,43))  #9
print(bisect_right(cargo_ascending,43))  #10

print(bisect_reverse_left(cargo_descending,7))  #2
print(bisect_reverse_right(cargo_descending,7))  #4
print(bisect_reverse_left(cargo_descending,43))  #0
print(bisect_reverse_right(cargo_descending,43))  #1
int bisect_reverse_left(vector<int>cargo_descending,int x,int lb=0,int ub=length){
    return length-(upper_bound(cargo_ascending.begin()+length-ub,cargo_ascending.begin()+length-lb,x)-cargo_ascending.begin());
}
int bisect_reverse_right(vector<int>cargo_descending,int x,int lb=0,int ub=length){
    return length-(lower_bound(cargo_ascending.begin()+length-ub,cargo_ascending.begin()+length-lb,x)-cargo_ascending.begin());
}

Since I received a comment, I will give you a version using the comparison function

#include <algorithm>
#include <iostream>
#include <vector>

int length;

//This is cargo_Only descending required
int bisect_reverse_left(std::vector<int>cargo_descending,int x,int lb=0,int ub=length){
    return *std::upper_bound(cargo_descending.begin()+lb,cargo_descending.begin()+ub, x, [](autoa,autob){returna>b;});
}
int bisect_reverse_right(std::vector<int>cargo_descending,int x,int lb=0,int ub=length){
    return *std::lower_bound(cargo_descending.begin()+lb,cargo_descending.begin()+ub, x, [](autoa,autob){returna>b;});
}



int main() {
    std::vector<int> v = {5, 4, 3, 2, 2, 1};
    length=v.size();
    std::cout<<bisect_reverse_left(v,3)<<std::endl;  //2
    std::cout<<bisect_reverse_right(v,3)<<std::endl;  //3
}

Recommended Posts

[python] [c ++] bisect (* _bound) in reverse (descending) list
Sorted list in Python
Next Python in C
List find in Python
C API in Python 3
Reverse strings in Python
Extend python in C ++ (Boost.NumPy)
Binary search in Python / C ++
python in mongodb in descending sort
Getting list elements in Python
Extract multiple list duplicates in Python
Difference between list () and [] in Python
[python] Manage functions in a list
Solve ABC036 A ~ C in Python
How to wrap C in Python
Solve ABC037 A ~ C in Python
Delete multiple elements in python list
Write C unit tests in Python
[python] Get the rank of the values in List in ascending / descending order
Linked list (list_head / queue) in C language
Display a list of alphabets in Python 3
Algorithm in Python (ABC 146 C Binary Search
Implement FIR filters in Python and C
OR the List in Python (zip function)
Difference between append and + = in Python list
Write O_SYNC file in C and Python
Download images from URL list in Python
Summary of built-in methods in Python list
Get the EDINET code list in Python
Run Python in C ++ on Visual Studio 2017
[Python] Understand list slicing operations in seconds
Asynchronous processing in Python: asyncio reverse lookup reference
How to use the C library in Python
Output formatted output in Python, such as C / C ++ printf.
[Python] Sort the list of pathlib.Path in natural sort
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
Make a copy of the list in Python
Boost.NumPy Tutorial for Extending Python in C ++ (Practice)
View drug reviews using a list in Python
I tried adding a Python3 module in C
Randomly select elements from list (array) in python
[Python] Manipulating elements in a list (array) [Sort]
First deep learning in C #-Imitating implementation in Python-
Flatten an irregular 2D standard list in Python
How to remove duplicate elements in Python3 list
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
nCr in python
N-Gram in Python
Programming in python