3

我需要找到毕达哥拉斯三元组的所有“a”和“b”值。例如,我会将数字指定为参数并为其查找所有毕达哥拉斯三元组。这是我的老师给我的一些示例代码:

>>> pytriples(5)
>>> [3,4,5] #would return this
>>> pytriples(25)
>>> [7,24,25] #would return this
>>> [15,20,25] #would return this

基本上,我需要编写 pytriples 程序,并且因为没有重复“a”和“b”而获得满分。这就是我开发的 - 问题是,我没有任何方法可以删除重复项。

这就是我所拥有的:

def pytriples(c):
    newlist = []
    for a in range(0, c):
        if ((c**2 - a**2)**0.5)%1 == 0:
            b = ((c**2 - a**2)**0.5)
            newlist.append([a,b,c])
    for i in newlist: #this part is supposed to remove the duplicates
        print i[0] #was used for debugging but I could not figure out why duplicates were not removed
        if i[0] >= i[1]:
            newlist.remove(i)
    return newlist
4

6 回答 6

1

不知道这是不是你想要的。。

您可以从三元组的元组列表中删除重复项,例如

考虑到你得到了所有的三胞胎list l

In [39]: l
Out[39]: [(1, 2, 3), (2, 3, 4), (2, 1, 3)]

要从中删除所有重复项,您可以使用

In [40]: set(map(tuple, [sorted(x) for x in l]))
Out[40]: set([(2, 3, 4), (1, 2, 3)])

然后您可以将其转换为列表以进行进一步处理

In [41]: list(set(map(tuple, [sorted(x) for x in l])))
Out[41]: [(2, 3, 4), (1, 2, 3)]

在你的情况下,

修改您在循环中迭代的列表是个坏主意,

因为一旦您删除假设 item1,item2 就会变成 item1,但是循环已经在 list 中迭代 item1,因此将跳过检查并且您将无法获得所需的输出

考虑一个小例子

In [43]: l
Out[43]: [(1, 2, 3), (2, 3, 4), (2, 1, 3)]

In [44]: for i in l:
   ....:     if i[0] == 2:
   ....:         l.remove(i)
   ....:

In [45]: l
Out[45]: [(1, 2, 3), (2, 1, 3)]
于 2012-12-07T03:37:49.847 回答
0

Instead of putting duplicates into the array and then using a pass to take them back out, just don't put them into it in the first place. You could change

        newlist.append([a,b,c])

to

        if a<b: newlist.append([a,b,c])

and delete all of the for i in newlist: loop and its contents.

(Note, a cannot equal b because sqrt(2) is irrational.)

于 2012-12-07T03:46:49.397 回答
0

我宁愿预先计算足够的三元组并尝试找到匹配项,而不是进行全面扫描。如果所有数字都小于给定的数字,我会即时计算更多:)

你的算法有一个正确的想法,但可以更简单地实现。一些基本的三角学完全摆脱了重复。

def pytriplets(hypotenuse):
    result = []
    # we only need to check half the triangles,
    # the rest are the same triangles with catheti swapped.
    # 0.71 is approximated sin(pi / 4). Biggest possible catheti are
    # in the isosceles triangle; no bigger should ever be checked. 
    # +1 is because range() excludes the top bound. 
    for a in range(1, int(hypotenuse * 0.71) + 1):
        hypo_squared = hypotenuse * hypotenuse
        a_squared = a * a
        # Square root will give us slight approximation errors;
        # explicitly make our other cathetus integer.
        b = int((hypo_squared  - a_squared) ** 0.5)
        if a_squared + b*b == hypo_squared:
            # A perfect match!
            result.append((hypotenuse, a, b)) # appending a tuple
    return result

print pytriplets(5)
print pytriplets(25)
于 2012-12-07T04:12:40.097 回答
0

应避免在迭代列表时修改列表。

可以通过简单的列表推导以您想要的方式删除元素:

newlist = [triplet for triplet in newlist if triplet[0] < triplet[1]]
于 2012-12-07T03:30:43.223 回答
0

导入数学

def main(): for x in range (1, 1000): for y in range (1, 1000): for z in range(1, 1000): if x*x == y*y + z*z and x +y+z ==1000:打印 y,z,x 打印 '-'*50

如果名称== '主要': main()

于 2013-09-02T18:55:11.623 回答
0

我生成了 c 以下所有完美正方形的列表,并使用来自列表两端的两个指针,我将尝试找到总和为 c^2 的数字对,直到它们交叉为止。

def pythogorian_triplets(c):
    c_square = c ** 2
    #list of perfect squares below c_square
    squares = [1]
    #populating the list
    for i in range(1, c - 1):
        squares.append(squares[-1] + (i << 1) + 1)

    i = 0
    j = c - 2
    l = c - 1

    while j >= i and i < l:
        while (squares[i] + squares[j] < c_square) and i < l and j > i:
            i = i + 1
        if squares[i] + squares[j] == c_square:
            print (i + 1, j + 1, c)
        j = j - 1

if __name__ == '__main__':
    pythogorian_triplets(int(raw_input()))
于 2012-12-07T04:49:53.070 回答