python - Project Euler #8 Python,我看不出代码有什么问题

标签 python

我一个月前开始使用 Python 编码,到目前为止我已经完成了其他 7 个欧拉问题,但在这个问题上已经坚持了一个星期了,这是问题陈述:

“1000 位数字中乘积最大的四个相邻数字是 9 × 9 × 8 × 9 = 5832。

找出 1000 位数字中乘积最大的 13 个相邻数字。这个产品的值(value)是什么?”

这是我到目前为止编写的代码:

num = '\
73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450'

digit = str(num)


def find(s, ch):
    return [i for i, ltr in enumerate(s) if ltr == ch]

lst = find(digit, "0")

placeholder = "0"
series_sum = "0"

for element in lst:
    total = 1
    value = digit[int(placeholder):element]
    if len(value) == 13:
        for i in value:
            total *= int(i)
        if total > int(series_sum):
            series_sum = str(total)
    elif len(value) > 13:
        c = 0
        c2 = 13
        counter = len(value) - 13
        while counter > 0:
            for i in value[c:c2]:
                total *= int(i)
            if total > int(series_sum):
                series_sum = str(total)
            counter -= 1
            c += 1
            c2 += 1
    placeholder = str(element)

print series_sum

我的方法是将数字转换为字符串,并创建一个包含 0 索引的列表,然后迭代该字符串并检查 0 之间是否至少有 13 个数字,然后从那里进行数学计算,但到目前为止,我只设法让它在前 13 个数字上工作,我认为错误可能出在以下部分: elif len(value) > 13:

此外,对于此类过程使用哪种类型的对象(列表、字符串、整数等)以及原因的任何建议,我们将不胜感激。

编辑:解决了!,非常感谢约翰,这是我使用的代码:

series_sum = 0

for i in range(len(num)):
    total = 1
    digits = num[i: i + 13]
    for i in digits:
        total *= int(i)
    if total > series_sum:
        series_sum = total

print series_sum

我现在发现我根本没有效率,至少我现在知道

最佳答案

您不需要变量digit,因为num已经是一个字符串。要从 num 中获取 13 个连续数字的所有 block ,您可以使用这样的循环

for i in range(len(num)):
    digits = num[i: i + 13]

最后你会得到一些较短的字符串,但它们不会影响你寻找最大产品的追求

关于python - Project Euler #8 Python,我看不出代码有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32813059/

相关文章:

python - Pandas - 在整数轴上按位置正确索引,在另一个轴上按标签正确索引(以避免链式分配)

javascript - 自动测试 Web 应用程序的框架

python - Polars read_excel 的日期格式问题

python - 狮身人面像 : Remove package and module name for specific function

python - 在球壳内生成积分点

python - “gcc”在尝试安装 gevent-websocket 时失败,退出状态为 1

python - 在 Django 中使用物化 View 或替代方案

Python交互式shell : how do I know what method gets used when I simply type the name of an object?

python - DataFrame 对象在计算长度时不可调用

python - 为什么这个神经网络的损失总是 0.0 而准确率总是 1.0?