由于某种原因,第一个循环中的x变量从代码int
变为str
。我很困惑,为什么必须这样做,但每次运行这个脚本时,我的设置最终都会被包含数百个零的字符串填充。xrange生成字符串?我不明白
如果有人想了解,这是解决欧拉的问题4.
# A palindromic number reads the same both ways.
# The largest palindrome made from the product
# of two 2-digit numbers is 9009 = 91 99.
# Find the largest palindrome made from the product of two 3-digit numbers.
def palindrome():
products = set()
for x in xrange(700,999):
for y in xrange(700,999):
temp = x*y
n = [x for x in str(temp)]
if temp not in products:
if len(n)%2 == 0:
half = len(n)/2
first = n[:half]
last = n[half:]
last.reverse()
if first == last:
products.add(temp)
return products
if __name__ == "__main__":
n = palindrome()
print n
听起来像功课。 – 2012-08-08 07:53:08
@InbarRose:OP说他在做[欧拉项目的问题#4](http://projecteuler.net/problem=4) – Kimvais 2012-08-08 08:09:36