Finding minimum sum and maximum sum of 5 number list

Question:

I came across an exercise on hackerrank to find the minimum sum and a maximum sum of a 5 number list and print the minimum sum and maximum sum.

For example, given the list [1, 3, 5, 7, 9], I sum 1 + 3 + 5 + 7 to get 16 and sum 3 + 5 + 7 + 9 to get 24. So, the answer is 16, and 24.

I get the correct answer for the example list above and also for [1, 2, 3, 4, 5] which is 10 and 14. But for [7, 69, 2, 221, 8974] it says the answer should be 299, and 9271. when I run my code I get 299, and 9266.

Where is my code wrong? Or is the question wrong? Thanks.

def miniMaxSum(arr):
    # Write your code here
    minSum = sum( arr[:-1])
    maxSum = sum( arr[1:])
    print(minSum,maxSum)
Asked By: larry8989

||

Answers:

Just add:

arr.sort()

above minSum. This will sort the list.

Answered By: Karol Milewczyk

This code works:

list1 = [7, 69, 2, 221, 8974]

largest = max(list1)

smallest = min(list1)


def MinMaxSum():
    sum_min = 0
    sum_max = 0
    for i in list1:
        if i == largest:
            continue
        else:
            sum_min += i

    for i in list1:
        if i == smallest:
            continue
        else:
            sum_max += i
       
    print(sum_min, sum_max)

MinMaxSum()

it returns 299 for min and 9271 for max

Answered By: Caleb Bunch
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.