python系列中最大的产品

6 python python-2.7

具有最大产品的1000位数字中的四个相邻数字是:

9 × 9 × 8 × 9 = 5832

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Run Code Online (Sandbox Code Playgroud)

找到具有最大产品的1000位数字中的十三个相邻数字.这个产品有什么价值?

我对这个问题的解决方案是:

def no(x):
    previous=0
    i=0
    t=1
    while i !=987:
        for num in x[i:i+13]:
            no=int(num)
            t=no*t

        if  t>previous:
            previous = t
        i=i+1
        t=1
    return previous  
Run Code Online (Sandbox Code Playgroud)

有没有其他好的有效方法来解决这个问题?因为我认为我的效率不高

Kas*_*mvd 5

您可以在max函数内使用生成器表达式,并使用适当的key函数来计算子数字的乘积.为此目的,您可以使用map函数将数字转换为整数和reduce(在python 3.X中functools.reduce)以计算整数的乘积.

>>> max((digits[i:i+13] for i in xrange(0, len(digits) - 12)), key=lambda x: reduce(mul, map(int, x)))
'5576689664895'
Run Code Online (Sandbox Code Playgroud)

请注意,如果您的数字之间有新的换行符,则需要使用str.replace()方法删除它们.

digits = digits.replace('\n', '')
Run Code Online (Sandbox Code Playgroud)

更优化的方法:

由于每次使用容器时都要处理13位数,以便在每次迭代中保留您的数字,这里最好的选择是deque()表单collections模块maxlen=13,它的弹出和推送操作的顺序是O(1).然后你可以计算出前13位数的乘积,并且在每次推送和弹出时你的初始产品应该被弹出的项目和多个按推项目除.并且在每次迭代中,您可以使用最大乘积保留序列.

from operator import mul
from collections import deque
from copy import copy

def cal_max_prod(container, current_product):
    max_container = {'seq': copy(container), 'prod': current_product}
    for i in digits[13:]:
        popped_item = int(container.popleft())
        container.append(i)
        try:
            push_item = int(i)
            current_product = (current_product / popped_item) * push_item
        except ZeroDivisionError:
            if '0' not in container:
                current_product = reduce(mul, map(int, container))
        else:
            if current_product > max_container['prod']:
                max_container['prod'] = current_product
                max_container['seq'] = copy(container)

    return ''.join(max_container['seq'])
Run Code Online (Sandbox Code Playgroud)

演示:

container = deque(digits[:13], maxlen=13)
current_product = reduce(mul, map(int, container))
print cal_max_prod(container, current_product)
5576689664895
Run Code Online (Sandbox Code Playgroud)