Implementation in C++  and python