![](/img/trans.png)
[英]Finding the largest palindrome product of two 3-digit numbers: what is the error in logic?
[英]Finding the largest palindrome of the product of two 3-digit numbers in Python
所以我試圖解決的挑戰是找到由兩個 3 位數字的乘積構成的最大回文數。 我是 Python 新手,所以我的代碼還不夠優雅或折射,但我似乎無法找到一個邏輯錯誤。
def ispalindrome(n):
rev_n = str(n)[::-1]
if n == rev_n:
return True
else:
return False
first_num = 100
second_num = 100
mylist=[]
while first_num < 1000:
while second_num < 1000:
item = first_num * second_num
mylist.append(item)
second_num += 1
second_num = 100
first_num +=1
# print (mylist)
num_as_string = []
for i in mylist:
i = str(i)
num_as_string.append(i)
print("Total products of two 3-digit numbers: {}").format(len(num_as_string))
print("-----------------------------------------------------")
def convert_to_num_list(string_list):
new_num_list = []
item = int(string_list)
new_num_list.append(item)
return new_num_list
palindrome_list = []
for j in num_as_string:
if ispalindrome(j) == True:
palindrome_list.append(j)
palindrome_list.sort()
# print(palindrome_list)
x = convert_to_num_list(j)
largest_palindrome = max(x)
print("Total palindroms of product of two 3-digit numers: {}").format(len(palindrome_list))
print("Largest palindrome = {}").format(largest_palindrome)
問題是我得到的最大回文數為 580085,即 995*583,但不是最大的回文數。 我相信最大的回文數是 906609,也就是 993*913,但是我的代碼沒有找到這個。 任何人都可以幫助我解決我的邏輯缺陷嗎?
你的代碼在數字和字符串之間做了很多不必要的轉換,這使得錯誤很難發現。 代碼中唯一需要字符串表示的地方是在確定數字是否為回文時。 所以這應該是代碼進行轉換的唯一地方。
邏輯錯誤在您的函數convert_to_num_list()
。 它接受一個數字的字符串表示,並返回一個包含該數字的 1 列表。 因此, "123321"
返回為[123321]
。 然后取那個 1-list 的max()
,它始終是傳遞給convert_to_num_list()
。 所以代碼永遠不會保留最大值,因為如果稍后出現較小的值,它將被覆蓋。 代碼報告995*583
是最大的,因為它晚於993*913
,而這又是因為995
> 993
。
您可以使用if
語句修復該錯誤,但該程序過於復雜並且很可能包含其他錯誤。 我建議將代碼簡化為生成最大回文的基本任務,而不打印出中間結果,因為代碼越簡單,就越容易看到邏輯錯誤。
def ispalindrome(n):
return str(n) == str(n)[::-1]
mylist=[]
for first_num in range(100,1000):
for second_num in range(100,1000):
item = first_num*second_num
if ispalindrome(item):
mylist.append(item)
print(max(mylist))
這給出了您預期的答案:
906609
這是一個函數,用於查找我在 stackoverflow 中找到的兩個 3 位數字的乘積的最大回文數。
鏈接到我發現的內容 - https://stackoverflow.com/a/7460573
def is_pal(c):
return int(str(c)[::-1]) == c
maxpal = 0
for a in range(999, 99, -1):
for b in range(a, 99, -1):
prod = a * b
if is_pal(prod) and prod > maxpal:
maxpal = prod
print maxpal
n1=999
n2=999
k=0
sl=[]
while n1>1:
count=n1
while count>=1:
result=n1*count
res=str(result)
res1=res[::-1]
if (res==res1):
sl.insert(k,result)
k+=1
count=count-1
n1=n1-1
print("largest pelindrom of 3 digit product is is %d" %(max(sl)))
palin=[]
for a in (range(1,1000)):
for b in (range(1,1000)):
d = a*b
d=str(d)
if len(d)>5:
if d[0]==d[5]:
if d[1]==d[4]:
if d[2]==d[3]:
palin.append(d)
palin.sort()
print(palin[len(palin)-1])
使用列表理解可以減少代碼行數,但我會提供一個替代選項,以便它更具可讀性。
List_of_palindromes = [i*j for i in range(100,1000) for j in range(i,1000) if str(i*j)==str(i*j)[::-1]]
print(max(List_of_palindromes))
更易讀的表格
List_of_palindromes = []
for i in range(100,1000):
for j in range(100,1000):
if str(i*j)==str(i*j)[::-1]:
List_of_palindromes.append(i*j)
print(max(List_of_palindromes))
看一下這個..! 這可以縮短,但這樣更容易理解(對於初學者)。
l = list(range(100, 1000))
m = list(range(100, 1000))
prod_max = 0
def is_palindrome(num):
temp = num
rev=0
while num > 0:
div = num%10
rev = rev*10 + div
num = num //10
if rev == temp:
return True
else:
return False
for i in range(len(l)):
for j in range(len(m)):
prod = l[i]*m[j]
if is_palindrome(prod) == True:
if prod > prod_max:
prod_max = prod
num1 = l[i]
num2 = m[j]
print(f'{prod_max} the product of {num1} and {num2} is a palindrome')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.