Skip to content

Latest commit

 

History

History
26 lines (21 loc) · 828 Bytes

sum_without_highest_and_lowest_number.md

File metadata and controls

26 lines (21 loc) · 828 Bytes

Description

Task

Sum all the numbers of a given array ( cq. list ), except the highest and the lowest element ( by value, not by index! ).

The highest or lowest element respectively is a single element at each edge, even if there are more than one with the same value.

Mind the input validation.

Example

{ 6, 2, 1, 8, 10 } => 16
{ 1, 1, 11, 2, 3 } => 6

Input validation

If an empty value (null, None, Nothing etc. ) is given instead of an array, or the given array is an empty list or a list with only 1 element, return 0.

My Solution

def sum_array(arr)
  return 0 if arr.nil? || arr.size <= 1
  arr.sum - (arr.min + arr.max)
end