'Minimum and maximum sums from a list Python

I've been doing problems on HackerRank to get my foot in the door for solving Python problems and while I've had fun working through a few, one problem is stumping me.

This problem is the Mini-Max sum, which takes an input(arr); an array of 5 integers, and prints the sum of the biggest and smallest 4 integers from arr in the next line's format via miniMaxSum(arr)

maximum minimum

e.g. miniMaxSum(1, 2, 3, 4, 5)

10 14

I've done something which you can find below that works with this, and most examples to return the desired results, but I've just found out that it doesn't work for arr = (5, 5, 5, 5, 5). I suspect that this is because when the maximum value is identical to another value in the list (e.g. arr = 1, 2, 3, 4, 4) or likewise for minimum (e.g. arr = 1, 1, 3, 4, 5), my code simply fails as it relies on nothing being the same as arr's biggest or smallest value. The HackerRank error message is "Wrong Answer" if that helps.

Please critique and suggest improvements so it works with any array of 5 integers (e.g. (5, 5, 5, 5, 5)). I am keen in understanding how this works and your help would be immensely appreciated. Thanks in advance!

# Complete the miniMaxSum function below.
def miniMaxSum(arr):
    listmax = []
    listmin = []
    for number in arr:
        if number > min(arr):
            listmax.append(number)    
    for number in arr:
        if number < max(arr):
            listmin.append(number)
    maxnum = sum(listmax)
    minnum = sum(listmin)
    print (minnum, maxnum)


Solution 1:[1]

Since it's a really small list, I would just sort it and then pick off the first-5 and then the last 5 and take the sums of those, respectively.

def miniMaxSum(arr):
  arr_sorted = sorted(arr)
  return sum(arr_sorted[:4]), sum(arr_sorted[-4:])

print(miniMaxSum([1,2,3,4,5]))
print(miniMaxSum([5,5,5,5,5]))

Output:

>>> print(miniMaxSum([1,2,3,4,5]))
(10, 14)
>>> print(miniMaxSum([5,5,5,5,5]))
(20, 20)

Solution 2:[2]

Try taking the overall sum and subtracting minimum or maximum:

def maxnum (x):
    return sum(x) - min(x)
def minnum(x):
    return sum(x) - max(x)

a = list(range(5))
b = [5]*5
print (a)
print (maxnum(a), minnum(a))
print (b)
print (maxnum(b), minnum(b))

output:

[0, 1, 2, 3, 4]

10 6

[5, 5, 5, 5, 5]

20 20

Solution 3:[3]

#!/bin/python3
# Complete the miniMaxSum function below.
def miniMaxSum(arr):
   arr=sorted(arr)
   s = sum(arr)
   print(s-arr[-1],s-arr[0])

Solution 4:[4]

It's not working with edge case arrays, say [5,5,5,5,5] because in when the program tries to pick out the 'listmax' and 'listmin', the program finds itself in a unique position where 'number' is neither greater than nor less than the max(arr). you can try to add a condition when each index is compared with the max so you don't miss out on repetitions of max/min values.

I went for computing all the sums and then choosing the min and max. Hope you find the code below helpful :

 def miniMaxSum(arr):
        sum_all = [] #list of sums of all the 4 elements
        temp = arr
        for i in range(5):
            p = temp.pop(i) #popping out an element to get an array of 4 elements 
            sum_all.append(sum(temp)) #calculating sum of all 4 elements
            temp.insert(i,p) #reset the list
        s_max = max(sum_all)
        s_min = min(sum_all)
        print(s_min , s_max) #prints the min and max of all the sums

Solution 5:[5]

I think this solution will be convenient for all test cases of the problem. so, please try it out and run.

def miniMaxSum(arr):

    max_elem = max(arr)
    min_elem = min(arr)

    mini = arr.copy()
    mini.remove(max_elem)

    maxi = arr.copy()
    maxi.remove(min_elem)

    sum_min = sum(mini)
    sum_max = sum(maxi)

    print(sum_min, sum_max)

if __name__ == '__main__':
    arr = list(map(int, input().rstrip().split()))
    miniMaxSum(arr)

Solution 6:[6]

pure logic -

def miniMaxSum(arr):
    sums=[]
    s=0
    for i in range(len(arr)):
        for j in range(len(arr)):
            s += arr[j] # suming all
        s = s-arr[i]
# remove current position value because u need sum of (size-1)
        sums.append(s)
        s=0
    low=high=sums[1] #assign first value for comparison
    for i in range(len(sums)):
        if low > sums[i]:
            low = sums[i]
        if high < sums[i]:
            high =sums[i]
    print(low,high)

I don't use keyword like short,sum. All test case done,Answer-

Test case 0
Test case 1
Test case 2
Test case 3
Test case 4
Test case 5
Test case 6
Test case 7
Test case 8
Test case 9
Test case 10
Test case 11
Test case 12
Test case 13
Test case 14
Compiler Message
Success
Input (stdin)

1 2 3 4 5
Expected Output

10 14

Solution 7:[7]

def minmax(n):
    a = sorted(n)
    mini = 0
    max = 0
    ll = len(n)
    for i in range(ll-1):
        max += n[i]
        mini += n[i+1]
    print(mini, max)
    
#  Driver Code
arr = [10, 9, 8, 7, 6, 5]
arr1 = [100, 200, 300, 400, 500]
    
minmax(arr)
minmax(arr1)

Solution 8:[8]

this would be easier

 arr = [5,5,5,5,5]
def miniMaxSum(arr):
    sum = 0
    for number in range(len(arr)):
        sum = sum +  arr[number]
    print (sum-max(arr), sum-min(arr))

Solution 9:[9]

arr = [1,2,3,4,5]
def miniMaxSum(arr):
    sorted_list = sorted(arr)
    print(sum(sorted_list[0:len(arr)-1]),sum(sorted_list[1:len(arr)]))

Solution 10:[10]

I like Ruslan's idea of finding the sum then subtracting the min or max to get each answer. However, instead of using 3 loops (one for sum, one for min, and one for max) we can do it all in one loop -

from math import inf

def minimum (a, b):
  return a if a < b else b

def maximum (a, b):
  return a if a > b else b

def minmaxsum (*xs):
  min = inf
  max = -inf
  sum = 0
  for x in xs:
    min = minimum (min, x)
    max = maximum (max, x)
    sum = sum + x
  return (sum - max, sum - min)

print (minmaxsum (1, 3, 5, 7, 9))
# (16, 24)

print (minmaxsum (1, 2, 3, 4, 5))
# (10, 14)

Solution 11:[11]

def miniMaxSum(a):   
    max = sum(list(filter(lambda x: x<max(a) if a.count(max(a))==1 else x<=max(a),a)))
    min = sum(list(filter(lambda x: x>min(a) if a.count(min(a))==1 else x>=min(a),a)))
    print(min,max)

This may help, and if not, can you update your expected output when your input is [1,1,2,3,4] or [1,2,3,4,4] or [5,5,5,5,5] ?