码迷,mamicode.com
首页 > 其他好文 > 详细

Exercise 3: Integer Right Triangles

时间:2020-05-28 00:59:06      阅读:86      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!