반응형
Your task is to sum the differences between consecutive pairs in the array in descending order.
Example
[2, 1, 10] --> 9
In descending order: [10, 2, 1]
Sum: (10 - 2) + (2 - 1) = 8 + 1 = 9
If the array is empty or the array has only one element the result should be 0 (Nothing in Haskell, None in Rust).
Solution:
def sort(func):
def wrapper(arr):
return func(sorted(arr, reverse=True))
return wrapper
@sort
def sum_of_differences(arr):
return sum(i - j for i, j in zip(arr, arr[1:]))
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
0925. Thinkful - Logic Drills: Traffic light (0) | 2022.09.25 |
---|---|
0924. Sum of positive (0) | 2022.09.24 |
0922. Love vs friendship (0) | 2022.09.22 |
0921. Sum without highest and lowest number (0) | 2022.09.21 |
0920. Difference of Volumes of Cuboids (0) | 2022.09.20 |