Completed counting triangles problem in python