此代碼旨在查找由兩個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