-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from MikeBeloborodov/8_kyu_sum_of_differences…
…_in_array 8 kyu sum of differences in array
- Loading branch information
Showing
4 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
### Sum of differences in array | ||
|
||
https://www.codewars.com/kata/5b73fe9fb3d9776fbf00009e/ | ||
|
||
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). |
7 changes: 7 additions & 0 deletions
7
8_kyu/sum_of_differences_in_array/sum_of_differences_in_array.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { sumOfDifferences } from "./sum_of_differences_in_array"; | ||
|
||
describe("test cases", () => { | ||
it("should return 9", () => expect(sumOfDifferences([1, 2, 10])).toBe(9)); | ||
it("should return 2", () => expect(sumOfDifferences([-3, -2, -1])).toBe(2)); | ||
it("should return 0", () => expect(sumOfDifferences([])).toBe(0)); | ||
}); |
8 changes: 8 additions & 0 deletions
8
8_kyu/sum_of_differences_in_array/sum_of_differences_in_array.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const sumOfDifferences = (arr: number[]): number => { | ||
return [...arr] | ||
.sort((a, b) => b - a) | ||
.reduce( | ||
(sum, next, indx, array) => (!indx ? 0 : sum + (array[indx - 1] - next)), | ||
0, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters