python algorithm examples translated to c++