最大的相邻数字 - 值增量不正确

时间:2017-05-17 17:14:48

标签: python increment

我正在编写这个程序,以找到这个数字中的13个相邻数字,当它们加在一起时,具有最大的总和。但是,当我运行它时,b值不会从12开始;它从一些极其高的数字开始,我无法弄清楚为什么。知道为什么我的a和b值没有正确递增吗?

num = "731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"

a = 0
b = 12
greatest = 0


while b != len(str(num)):
    num = str(num)
    newNum = num[a:b]
    total = 0
    for num in newNum:
        num = int(num)
        total += num
    if total > greatest:
        greatest = total
    a+=1
    b+=1
    print(b)

print(greatest)

2 个答案:

答案 0 :(得分:1)

主要问题是您在内部循环中重复使用num,这会在首次运行后呈现“原始”num错误。

此外,如果您想要13个数字导入,最好从b = 13

开始

此外,不需要str(num),因为它已经是一个字符串,无需在程序中更改b。您还可以在sum上使用map替换内部循环。

以下是这些变化后的样子:

num = "731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"

index = 0
run_in = 13
greatest = 0

while index + run_in < len(num):
    num_slice = num[index: index + run_in]
    slice_sum = sum(map(int, num_slice))

    if slice_sum > greatest:
        greatest = slice_sum

    index += 1

print(greatest)

如果您使用超级函数,则可以使用列表推导和max闭包创建相同的效果,迭代所有可能的索引(直到数字的长度减去运行):

greatest = max(sum(map(int, num[index: index + run_in])) for index in range(len(num) - run_in))

答案 1 :(得分:0)

def largest(num, k):
    num = str(num)
    if len(num) < k: raise ValueError("Need a number with at least {} digits".format(k))
    currsum = sum(int(i) for i in num[:k])
    answer = currsum, 0
    i = k+1
    while i < len(num):
        currsum -= int(num[i-k])
        currsum += int(num[i])
        if currsum > answer[0]: answer = currsum, i
        i += 1
    return answer

total, ind = largest(myNum, 13)
print("The maximum sum of digits is {}, starting at index {}".format(total, ind))