Tam211
Tam211

Reputation: 743

Maximum sum of sublist with a specific length

I'm supposed to write a function which takes two numbers, the first is a given number, and the second is the length for the maximum sublist that I'm supposed to find: for example input (1234,2) the output would be 7

this is my code so far, it just computes the sum of the entire digits:

def altsum_digits(n,d):
b=str(n)
c=[]
for digit in b:
    c.append(int(digit))

maxthere=0
realmax=0
for a in str(d):             
    for i in c:
        maxthere=max(0,(maxthere+int(i)))
        realmax=max(maxthere,realmax)
        maxthere==0
print(realmax)

Upvotes: 0

Views: 840

Answers (6)

let's say you get a number n, and a length k.

What you have to do is first turn n into a list of numbers, and then use a sliding window of size k where at each step you add the next number, and substract the first one in the sliding window, and keep track of the max_sum so you can return it at the end.

The function would look something like this

def altsum_digits(n, k):
    list_n = [int(x) for x in str(n)]
    max_sum = sum(list_n[:k])

    for i in range(k, len(list_n)):
        current_sum = current_sum + list_n[i] - list_n[i - k]
        max_sum = max(current_sum, max_sum)

    return max_sum

It's an O(n) solution, so it's a lot better than generating all sublists of size k. Hope it helps!

Upvotes: 1

Daniel Simpkins
Daniel Simpkins

Reputation: 684

I think I understand what you're asking, and here is my solution. I've tested it on your input as well as other inputs with varying lengths of substring. This code finds the maximum sum of adjacent substrings in the input.

def sum_of_sublist(input, maxLength):
    input_array = [int(l) for l in str(input)]
    tempMax = 0
    realMax = 0
    for i in range(len(input_array) - (maxLength - 1)):
        for inc in range(0, maxLength):
            tempMax += input_array[i+inc]
        if tempMax > realMax:
            realMax = tempMax
        tempMax = 0
    print realMax


sum_of_sublist(1234, 2)

So, for an input for the call sum_of_sublist(1234, 2), it will print the value 7 because the largest sum of 2 consecutive numbers is 3 + 4 = 7. Similarly, for the callsum_of_sublist(12531, 3), the program will print 10 because the largest sum of 3 consecutive numbers is 2 + 5 + 3 = 10.

Upvotes: 0

Taxellool
Taxellool

Reputation: 4322

By what i get from question, this should do what you want:

def do(n, d):
    print sum(sorted([int(x) for x in str(n)])[-d:])

Upvotes: 1

Drewness
Drewness

Reputation: 5072

This should do the trick:

def altsum_digits(n, d):
    l = list(map(int, str(n)))
    m = c = sum(l[:d])
    for i in range(0, len(l)-d):
        c = c - l[i] + l[i+d]
        if c > m: m = c
    print m

altsum_digits(1234,2)
>>> 7

Upvotes: 0

Hugh Bothwell
Hugh Bothwell

Reputation: 56684

def sublists(lst, n):
    return (lst[i:i+n] for i in range(len(lst) - n + 1))

def max_sublist_sum(lst, n):
    return max(sum(sub) for sub in sublists(lst, n))

max_sublist_sum([1,2,3,4], 2)   # => 7

Upvotes: 0

roippi
roippi

Reputation: 25974

Let's clarify to make sure we're on the same page.

Inputs: 1) a list li of digits; 2) n

Output: the slice from li of length n that has maximal sum.

li = [4,2,1,7,1,3,8,4,7,8,1]

n = 2

slices = (li[x:x+n] for x in range(len(li)-n+1))

max(map(sum,slices))
Out[113]: 15

Upvotes: 0

Related Questions