使用 numpy 查找由两个 3 位数的乘积构成的最大回文

当我尝试使用 numpy 包解决这个问题时,我陷入了这个问题。我的想法是,我将对 100 到 999 之间的 3 位数字所做的所有计算相乘并保留一个列表,然后检查列表以查看哪些是回文并保存它们。最后,我将对列表进行排序并获得最大的回文。下面的代码显示了我试图做的事情。


import numpy as np


def void():

    list1 = np.array(range(100,999))

    list2 = np.array(range(100,999))

    k = []

    

    for i,j in zip(list1,list2):

        k.append(np.multiply(list1,list2))

        

    b = []

    

    for x in range(0,len(k)):

        if(reverseNum(k[x])==k[x]):

            b.append(k[x])

            

    print(b)

    print(b[-1])

    

def reverseNum(num):

    rev = 0

    

    while(num>0):

        rem = num % 10

        rev = (rev*10) +rem

        num = num // 10

        

    return rev  

    

void()

但是,当我尝试检查列表中的数字是否为回文时,出现以下错误:


Traceback (most recent call last):                                                                                    

  File "main.py", line 40, in <module>                                                                                

    void()                                                                                                            

  File "main.py", line 22, in void                                                                                    

    if(reverseNum(k[x]),k[x]):                                                                                        

  File "main.py", line 31, in reverseNum                                                                              

    while(num>0):                                                                                                     

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()  

这是否意味着无法使用 numpy 作为解决此问题的方法?如果是,我哪里错了?


编辑:到目前为止我已经尝试过的(因为它被问到):根据错误消息,我尝试使用np.equalas 以及而np.greater不是检查但它给出了相同的错误。if(reverseNum(k[x])==k[x])num>0


波斯汪
浏览 120回答 4
4回答

白板的微信

假设结果有六位数的 NumPy 方法(它不能有更多,因为 999 2是 998001):import numpy as npv = np.arange(100, 1000)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# the range of three-digit numbersa = np.outer(v, v)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# all the productsprint(a[(a // 100000 == a % 10) &&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # first digit == sixth digit&nbsp; &nbsp; &nbsp; &nbsp; (a // 10000 % 10 == a // 10 % 10) &&nbsp; &nbsp; &nbsp; &nbsp; (a // 1000 % 10 == a // 100 % 10)].max())版画906609。使用纯 Python 进行双重检查:>>> max(x*y&nbsp; &nbsp; &nbsp; &nbsp; for x in range(100, 1000)&nbsp; &nbsp; &nbsp; &nbsp; for y in range(100, 1000)&nbsp; &nbsp; &nbsp; &nbsp; if str(x*y) == str(x*y)[::-1])906609

紫衣仙女

另一个真正的 NumPy 解决方案,使用您的方式反转数字(主要是按照.any()错误消息中的建议修复它,您固执地拒绝尝试)。v = np.arange(100, 1000)a = np.outer(v, v)num = a.copy()rev = num * 0while (m := num > 0).any():&nbsp; &nbsp; rev[m] = rev[m] * 10 + num[m] % 10&nbsp; &nbsp; num[m] //= 10print(a[rev == a].max())没有面具m你会得到相同的结果 (906609),但它更安全。否则五位数的乘积不能正确反转,比如 101*102=10302 变成 203010 而不是 20301。

慕尼黑的夜晚无繁华

为什么它必须使用 numpy?# test if palindrome based on strdef is_palindrome(number: int):&nbsp; &nbsp; converted_to_string = str(number)&nbsp; &nbsp; return converted_to_string == converted_to_string[::-1]# product of two three-digit numbersyou_right = []values = []for x in range(999, 99, -1):&nbsp; &nbsp; for y in range(999, 99, -1):&nbsp; &nbsp; &nbsp; &nbsp; product = x*y&nbsp; &nbsp; &nbsp; &nbsp; if is_palindrome(product):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values.append((x, y))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; you_right.append(product)winner = you_right.index(max(you_right))print(values[winner])# output(993, 913)

精慕HU

您的问题源于您的行,包括zip. 我下面的代码并不漂亮,但尝试松散地遵循您的方法。import numpy as npdef void():&nbsp; &nbsp; list1 = np.array(range(100,1000))&nbsp; # you want to include '999'&nbsp; &nbsp; list2 = np.array(range(100,1000))&nbsp; &nbsp; k = []&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for i,j in zip(list1,list2):&nbsp; &nbsp; &nbsp; &nbsp; k.append(np.multiply(list1,j))&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; b = []&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for r, row in enumerate(k):&nbsp; &nbsp; &nbsp; &nbsp; for c, cell in enumerate(row):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if reverseNum(cell)==cell:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.append(cell)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; print(b)&nbsp; &nbsp; print(max(b))&nbsp; &nbsp;&nbsp;def reverseNum(num):&nbsp; &nbsp; rev = 0&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; while(num>0):&nbsp; &nbsp; &nbsp; &nbsp; rem = num % 10&nbsp; &nbsp; &nbsp; &nbsp; rev = (rev*10) +rem&nbsp; &nbsp; &nbsp; &nbsp; num = num // 10&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return rev&nbsp; &nbsp;&nbsp;void()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python