Start learning free

LESSON 270 OF 673 · TUPLES, SORTING & BUILT-INS

Total Without Extremes in Python: sum, min and max

Practice Read the full explanation

You get a list of integers nums. Return their sum without the smallest and the largest value.

Some contests score this way. Each judge gives a score. The lowest and the highest score are dropped. So one very strict or very kind judge can't decide alone.

Drop only one smallest value and one largest value. If the smallest value appears twice, the other copy still counts. The same goes for the largest value.

 

Example 1:

Input: nums = [7, 9, 6, 8, 10]
Output: 24
Explanation: The smallest value is 6 and the largest is 10.
The rest add up to 7 + 9 + 8 = 24.

Example 2:

Input: nums = [5, 1, 5, 9, 1]
Output: 11
Explanation: The smallest value, 1, appears twice.
Only one 1 is dropped, and so is the 9.
5 + 5 + 1 = 11.

Example 3:

Input: nums = [-4, 3, -2]
Output: -2
Explanation: -4 is the smallest value and 3 is the largest.
Only -2 is left.

 

Constraints:

  • nums has 3 to 100 numbers.
  • Every number is between -1000 and 1000.
STEP 1 OF 4
    Ctrl + Enter to check

    PYTHON · TUPLES, SORTING & BUILT-INS

    Trimmed Total in Python, explained

    Problem overview

    The method trimmedTotal gets a list nums of 3 to 100 integers and returns their sum without the smallest and the largest value. It works like a contest where each judge gives a score, and the lowest and the highest are dropped so that one very strict or very kind judge can't decide alone.

    Only one smallest and one largest value are dropped. If the smallest value appears twice, the other copy still counts, and the same is true for the largest.

    For [7, 9, 6, 8, 10] the smallest is 6 and the largest is 10, so the answer is 7 + 9 + 8 = 24. For [5, 1, 5, 9, 1] the answer is 11: one 1 and the 9 are dropped, and 5 + 5 + 1 remains.

    Approach

    You don't need to remove anything from the list. Add up everything, then subtract the two extremes once each: total - smallest - largest. Subtracting a value once takes exactly one copy of it out of the sum, so repeated extremes are handled correctly without extra code.

    Python has a built-in for each part: sum for the total, min for the smallest value and max for the largest.

    Step by step

    1. Store sum(nums) in total.
    2. Store min(nums) in smallest. It returns the value once, even if the value appears twice.
    3. Store max(nums) in largest.
    4. Return total - smallest - largest.

    The solution in Python

    class Solution:
        def trimmedTotal(self, nums: List[int]) -> int:
            total = sum(nums)
            smallest = min(nums)
            largest = max(nums)
            return total - smallest - largest
    

    The names total, smallest and largest were picked on purpose. Calling the total sum in a file where the built-in is used again later would hide it: the name would hold a number, and calling it fails with a TypeError.

    Tracing the examples

    numssumminmaxanswer
    [7, 9, 6, 8, 10]4061024
    [5, 1, 5, 9, 1]211911
    [-4, 3, -2]-3-43-2

    The third row has a negative sum and a negative answer; the same arithmetic works. The list has at least 3 numbers, so min and max never see an empty list.

    Another way

    Sorting also works: sum(sorted(nums)[1:-1]) drops the first and the last element of the sorted list, and gives 24 for the first example and 11 for the second. It sorts the whole list to drop two values, so the three built-ins are simpler, and they pass over the list without reordering it.

    Edge cases

    The list always has at least three numbers, so after dropping two values, at least one remains. With exactly three numbers, the answer is the middle value, as in [-4, 3, -2], where -2 is left. If all the numbers are equal, say three 5s, then the smallest and the largest are both 5, one copy of each is dropped, and one 5 remains, which 15 - 5 - 5 gives correctly. The numbers are small (between -1000 and 1000), and Python integers don't overflow anyway.

    Common mistakes

    • Removing every copy of the smallest value, for example by filtering with if num != smallest. In [5, 1, 5, 9, 1] that drops both 1s and gives 10, not 11.
    • Forgetting to subtract the largest value as well as the smallest.
    • Naming a variable sum, min or max and then calling the built-in of that name.
    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free