Add triangles algorithms in python with full explanation.