Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带有错误消息的Euler项目问题_Python - Fatal编程技术网

Python 带有错误消息的Euler项目问题

Python 带有错误消息的Euler项目问题,python,Python,Euler项目问题4: 回文数字的两种读取方式相同。 由两个两位数的乘积构成的最大回文是9009=91×99。 查找由两个3位数字的乘积构成的最大回文 以下是我的代码并收到错误消息: Traceback (most recent call last): File "<input>", line 10, in <module> TypeError: 'int' object is not iterable 塞卢克的观点是正确的,你也可以将范围设置为10,因为这是上限,

Euler项目问题4: 回文数字的两种读取方式相同。 由两个两位数的乘积构成的最大回文是9009=91×99。 查找由两个3位数字的乘积构成的最大回文

以下是我的代码并收到错误消息:

Traceback (most recent call last):
  File "<input>", line 10, in <module>
TypeError: 'int' object is not iterable

塞卢克的观点是正确的,你也可以将范围设置为10,因为这是上限,所以0-9是范围10

In [67]:
...:
...:
...: _list = []
...:
...: for a in range(10):
...:     for b in range(10):
...:         for c in range(10):
...:             _list.append(palindromic(a, b, c))
...:
...: largest =  max(_list)
...: print(largest)
...:
...:
999999

并且不断地问问题,有些答案听起来可能很挑剔,但公正的目标是更加精确和有效-
玩得开心

@AlexanderBrockmeier answer更正操作代码中的错误

但是,问题是OP算法不正确,因此更正代码会产生错误的答案

通过将JavaScript转换为Python,得到了一个有效的算法

def is_palin(i):
  """ Return True if a number is a Palindrome, False otherwise """
  s = str(i)
  return s == s[::-1]

def largestPalindrome():
    """ Finds largest Palindrome made by multiplying 2 three digit numbers """
    # algorithm basically tries all pairs of 3 digit numbers (i.e. 101 to 999)
    arr = []
    for i in range(999, 100, -1):
      for j in range(999, 100, -1):
        mul = j * i
        if is_palin(mul):
          arr.append((mul, j, i)) 

    return max(arr, key = lambda x: x[0])  # max based upon product


result = largestPalindrome()
print(f'Largest number is {result[0]}, with factors of {result[1]} x {result[2]}')
输出

原贴OP算法的问题

从@AlexanderBrockmeier answer中,我们可以看到操作代码生成的答案是999999

但是,这没有两个3位数的系数,因为其系数为:

1 × 999999 = 999,999
3 × 333333 = 999,999
7 × 142857 = 999,999
9 × 111111 = 999,999
11 × 90909 = 999,999
13 × 76923 = 999,999
21 × 47619 = 999,999
27 × 37037 = 999,999
33 × 30303 = 999,999
37 × 27027 = 999,999
39 × 25641 = 999,999
63 × 15873 = 999,999
77 × 12987 = 999,999
91 × 10989 = 999,999
99 × 10101 = 999,999
111 × 9009 = 999,999
117 × 8547 = 999,999
143 × 6993 = 999,999
189 × 5291 = 999,999
231 × 4329 = 999,999
259 × 3861 = 999,999
273 × 3663 = 999,999
297 × 3367 = 999,999
333 × 3003 = 999,999
351 × 2849 = 999,999
407 × 2457 = 999,999
429 × 2331 = 999,999
481 × 2079 = 999,999
693 × 1443 = 999,999
777 × 1287 = 999,999
819 × 1221 = 999,999
999 × 1001 = 999,999

这是修订后的守则

def palindromic(a, b, c):
    result = a * 10 ** 5 + b * 10 ** 4 + c * 10 ** 3 + c * 10 ** 2 + b * 10 + a * 1
    return result

_list = []

for a in range(10):
    for b in range(10):
        for c in range(10):
            _list.append(palindromic(a, b, c))

print(max(_list))

import time
start_time = time.time()
print("--- %s seconds ---" % (time.time() - start_time)) 

请始终发布完整的错误消息并进行完整的回溯。1您混淆了append和extend 2。append和。extend在适当的位置修改列表;它们不返回值,3也不使用内置的变量名,如list。for循环是否应该使用range10而不是range9,即使用0到9之间的数字?即使更正了编码错误,请参见@alexanderBrockmeier answer,由于使用了不正确的算法,此代码生成的结果999999不正确。此产品不是999999而不是888888吗?是的,我们称之为输入错误;
1 × 999999 = 999,999
3 × 333333 = 999,999
7 × 142857 = 999,999
9 × 111111 = 999,999
11 × 90909 = 999,999
13 × 76923 = 999,999
21 × 47619 = 999,999
27 × 37037 = 999,999
33 × 30303 = 999,999
37 × 27027 = 999,999
39 × 25641 = 999,999
63 × 15873 = 999,999
77 × 12987 = 999,999
91 × 10989 = 999,999
99 × 10101 = 999,999
111 × 9009 = 999,999
117 × 8547 = 999,999
143 × 6993 = 999,999
189 × 5291 = 999,999
231 × 4329 = 999,999
259 × 3861 = 999,999
273 × 3663 = 999,999
297 × 3367 = 999,999
333 × 3003 = 999,999
351 × 2849 = 999,999
407 × 2457 = 999,999
429 × 2331 = 999,999
481 × 2079 = 999,999
693 × 1443 = 999,999
777 × 1287 = 999,999
819 × 1221 = 999,999
999 × 1001 = 999,999
def palindromic(a, b, c):
    result = a * 10 ** 5 + b * 10 ** 4 + c * 10 ** 3 + c * 10 ** 2 + b * 10 + a * 1
    return result

_list = []

for a in range(10):
    for b in range(10):
        for c in range(10):
            _list.append(palindromic(a, b, c))

print(max(_list))

import time
start_time = time.time()
print("--- %s seconds ---" % (time.time() - start_time))