标签:returns point poi range ISE find assert stc hat
Given a perimeter of 60, we can find two right triangles with integral length sides: [(10, 24, 26), (15, 20, 25)]. Complete the following function, which takes an integer p
and returns the number of unique integer right triangles with perimeter p
.
Note that your solution should take care to limit the number of triangles it tests --- your function must complete in under 3 seconds for all values of p
used in the test cells below to earn credit.
def integer_right_triangles(p): i=0 for x in range(p//3,p//2): for y in range((p-x)//2,(p-x)): if x<y: break z = p-x-y if y<z: continue if x*x == y*y + z*z: i+=1 return i
# (2 points)
import unittest
tc = unittest.TestCase()
tc.assertEqual(integer_right_triangles(60), 2)
tc.assertEqual(integer_right_triangles(100), 0)
tc.assertEqual(integer_right_triangles(180), 3)
Exercise 3: Integer Right Triangles
标签:returns point poi range ISE find assert stc hat
原文地址:https://www.cnblogs.com/ladyrui/p/12977426.html