此代码旨在查找由两个3位数字的乘积创建的最大回文。为什么由代码创建的列表不包含大于99,999(Python)的值
我敢肯定,有更有效的方式来解决这个问题,欢迎您发表他们,但在这个阶段我的学习,我最感兴趣的是我怎么能编辑的代码,我已经写出来使其正确工作。
当我运行此代码时,它正确地创建了一个排序的回文列表,但列表中最大的数字是99,999。我不明白为什么名单不会超过这个。
def palindromes():
product_list=[]
palindrome_list=[]
for a in range(100,1000):
for b in range(100,1000):
product_list.append(a*b)
for product in product_list:
product = str(product)
if len(product) % 2 == 0:
if product[0]==product[5] and product[1]==product[4] and product[2]==product[3]:
palindrome_list.append(product)
if len(product) % 2 != 0:
if product[0]==product[4] and product[1]==product[3]:
palindrome_list.append(product)
palindrome_list = sorted(set(palindrome_list))
return palindrome_list
print(palindromes())
Dupe? http://stackoverflow.com/questions/7460545/palindrome-from-the-product-of-two-3-digit-numbers?rq=1 – BenDundee 2013-04-04 22:37:18
不是真的,运不明白为什么这个列表是有限100.000元件 – 2013-04-04 22:38:43
这很有帮助,但如果可能的话,我想要对我的特定代码提供反馈。仍然赢得该语言,我想看看我的代码出错了。这不是一个好的地方吗? – 2013-04-04 22:39:46