简体   繁体   English

找出由两个 2 位数字的乘积构成的最大回文

[英]To find the largest palindrome made from the product of two 2-digit numbers

def largest_palindrome(n1,n2):
    mylist = [x for x in range(n1,n2)]
    for y in mylist:
        if y == y[::-1]:
            print(y)
        else:
            pass    

largest_palindrome(100,9801) large_palindrome(100,9801)

When I am executing this code the error that comes up is TypeError: 'int' object is not subscriptable .当我执行此代码时,出现的错误是TypeError: 'int' object is not subscriptable I need to know what is the problem is in this code and what changes will be done to make this code running.我需要知道这段代码中的问题是什么,以及要进行哪些更改才能使这段代码运行。

You need to cast to string to be able to reverse and compare:您需要转换为字符串才能反转和比较:

def largest_palindrome():                        # <-- arguments are not needed
    for y in (x for x in range(9801, 100, -1)):  # use a generator, that iterates from largest to smallest. 
        if str(y) == str(y)[::-1]:
            return y                             # early exit when largest match is found (it will be the first)

print(largest_palindrome())

spoiler alert:剧透警告:

9779

as a one liner:作为单班轮:

max([x for x in range(9801, 100, -1) if str(x) == str(x)[::-1]])

As a one liner generator作为一个线性发生器

(thanks @Austin in the comments): (感谢@Austin 在评论中):

next(x for x in range(9801, 100, -1) if str(x) == str(x)[::-1])

Reblochon's answer doesn't solve the problem as it only iterates between the smallest and the biggest number that could come from two two-digit numbers. Reblochon 的答案并没有解决问题,因为它只在可能来自两个两位数的最小和最大数字之间进行迭代。 It doesn,t iterate through two-digit numbers.它不会遍历两位数。

def largest_palindrome():
    lastBiggestProduct = 0;
    lastBiggestNumb = 10;
    for firstNum in range(10,100):
        a = list(range(lastBiggestNumb,firstNum))
        a.extend(range(firstNum+1,100))
        for secondNum in a:
            prod = firstNum*secondNum
            if(prod>lastBiggestProduct and str(prod) == str(prod)[::-1]):
                lastBiggestProduct = firstNum*secondNum
                lastBiggestNumb = secondNum
    return lastBiggestProduct


print(largest_palindrome())

That returns:那返回:

9009

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 numpy 找到由两个 3 位数的乘积组成的最大回文 - Find the largest palindrome made from the product of two 3-digit numbers using numpy 查找由两个3位数组成的乘积所产生的最大回文 - Find the largest palindrome made from the product of two 3-digit numbers, just a small edit needed 如何在python中找到由两个三位数组成的最大回文? - How to find the largest palindrome made of two three digit numbers in python? 在我的计算中,要从两个3位数的乘积中得到最大的回文数,在哪里出错? - where am I going going wrong in my calculations to get largest palindrome made from the product of two 3-digit numbers? 在Python中找到两个三位数乘积的最大回文 - Finding the largest palindrome of the product of two 3-digit numbers in Python 从python中3个数字的乘积中找到最大的回文 - find the largest palindrome from the product of 3 numbers in python 最大回文集,是两个n位数字的乘积(Python) - Largest palindrome which is product of two n-digit numbers (Python) 来自两个3位数的乘积的回文 - Palindrome from the product of two 3-digit numbers 找到两个三位数的最大回文乘积:逻辑错误是什么? - Finding the largest palindrome product of two 3-digit numbers: what is the error in logic? 欧拉计划:两个三位数数字的最大回文产品 - Project Euler: Largest palindrome product of two 3-digit number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM