반응형
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
.
Solution:
from typing import Optional, List
def sum_array(arr: Optional[List[int]]):
return arr and sum(sorted(arr)[1:-1]) or 0
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
0923. Sum of differences in array (0) | 2022.09.23 |
---|---|
0922. Love vs friendship (0) | 2022.09.22 |
0920. Difference of Volumes of Cuboids (0) | 2022.09.20 |
0919. Cat years, Dog years (0) | 2022.09.19 |
0918. Grasshopper - Array Mean (0) | 2022.09.19 |