Python 寻找勾股三胞胎
我需要找到毕达哥拉斯三元组的所有“a”和“b”值。例如,我将数字指定为一个参数,并找到它的所有毕达哥拉斯三元组。下面是老师给我的一些示例代码:Python 寻找勾股三胞胎,python,algorithm,Python,Algorithm,我需要找到毕达哥拉斯三元组的所有“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(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
在对列表进行迭代时修改列表是错误的 通过简单的列表理解,可以按您想要的方式删除元素:
newlist = [triplet for triplet in newlist if triplet[0] < triplet[1]]
newlist=[triplet for triplet for triplet in newlist if triplet[0]
不确定这是否是您想要的
您可以从三元组的元组列表中删除重复项,如
假设您在列表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)]
就你而言
在循环中修改正在迭代的列表是个坏主意
因为一旦您删除了假设项1,项2就变成了项1,但循环已经在列表中迭代了项1,所以检查将被跳过,您将无法获得所需的输出
举个小例子
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)]
与其将重复项放入数组中,然后使用传递将其取出,只需首先不要将它们放入数组中即可。你可以改变
newlist.append([a,b,c])
到
如果a而不是进行完整扫描,我宁愿预先计算足够多的三元组并尝试找到匹配项。如果所有的数字都小于给定的数字,我会动态计算更多:)
您的算法思路正确,但可以实现得更简单。一些基本的三角学使你完全摆脱了重复
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)
我将生成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()))
def pythogorian_三联体(c):
c_平方=c**2
#c_square下方的完美正方形列表
正方形=[1]
#填充列表
对于范围(1,c-1)内的i:
正方形。附加(正方形[-1]+(i=i和ii:
i=i+1
如果平方[i]+平方[j]==c_平方:
打印(i+1,j+1,c)
j=j-1
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
pythogorian_三元组(int(raw_input()))
导入数学
def main():
对于范围(1000)内的x:
对于范围(1000)内的y:
对于范围(1000)内的z:
如果x*x==y*y+z*z和x+y+z==1000:
打印y,z,x
打印'-'*50
如果name='main':
main()
通常是删除重复项的一种好方法,事实上我们有关于重复项结构的信息,在这种情况下(triplet[0]>=triplet[1]
),可能会使列表理解方法更快。+1,尽管如此。:)我还没有了解到这一点,但从我的代码中,我只需要知道为什么它不在for循环中删除列表元素。@AlexMann:修改循环中迭代的列表是个坏主意,而只是创建一个不同的列表,在循环中向其追加所需的值,然后返回/打印
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()))