在Python中找到两个3位数字的乘积的最大回文 [英] Finding the largest palindrome of the product of two 3-digit numbers in Python
问题描述
所以我要解决的难题是找到由两个3位数字的乘积组成的最大回文.我是Python的新手,所以我的代码尚不完善或精简,但是似乎找不到逻辑错误.
So the challenge I'm trying to solve is to find the largest palindrome made from the product of two 3-digit numbers. I'm new to Python and so my code is not elegant or refracted yet, but there is a logical error that I can't seem to find.
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,但是我的代码找不到此回文.谁能帮助我解决我的逻辑缺陷?
The problem is that I'm getting the largest palindrome as 580085, which is 995*583 but is NOT the largest palindrome. I believe the largest palindrome is 906609, which is 993*913, but my code is not finding this. Can anyone help me with the flaw in my logic?
推荐答案
您的代码在数字和字符串之间进行了很多不必要的转换,从而使错误很难发现.代码中唯一需要字符串表示形式的地方是确定数字是否为回文式时.因此,这应该是代码进行转换的唯一位置.
Your code does a lot of unnecessary conversion between numbers and strings, which made the error hard to find. The only place in the code that needs a string representation is when determining if the number is a palindrome or not. So that should be the only place that the code does the conversion.
函数convert_to_num_list()
中存在逻辑错误.它采用 one 数字的字符串表示形式,并返回一个包含该数字的1-list.因此,"123321"
返回为[123321]
.然后,您采用该1-list的max()
,该值始终是传递给convert_to_num_list()
的值.因此,代码永远不会保留最大值,因为如果以后输入较小的值,它将被覆盖.该代码将995*583
报告为最大,因为它出现在993*913
之后,这又是因为995
> 993
.
The logic error is in your function convert_to_num_list()
. It takes a string representation of one number and returns a 1-list containing that number. So, "123321"
gets returned as [123321]
. You then take the max()
of that 1-list, which is always the value that was passed to convert_to_num_list()
. So the code never keeps the largest value because if a smaller value comes in later it will be overwritten. The code reports 995*583
as the largest because it comes in later than 993*913
, which in turn is because 995
> 993
.
您可以使用if
语句修复该错误,但是该程序过于复杂并且可能包含其他错误.我建议将代码简化为产生最大回文的基本任务,而不必打印出中间结果,因为代码越简单,就越容易发现逻辑错误.
You can fix that error with an if
statement, but the program is overcomplicated and may well contain other bugs. I recommend reducing the code to the essential task of producing the largest palindrome, without printing out the intermediate results, because the simpler the code the easier it is to see a logic error.
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))
这给出了您所期望的答案:
This gives your expected answer:
906609
这篇关于在Python中找到两个3位数字的乘积的最大回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!