-
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.
- Loading branch information
Showing
5 changed files
with
34 additions
and
3 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,13 @@ | ||
## 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). |
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 @@ | ||
import { sumOfDifferences } from "./index"; | ||
|
||
describe("Tests", () => { | ||
it("example", () => { | ||
expect(sumOfDifferences([1, 2, 10])).toBe(9); | ||
expect(sumOfDifferences([-3, -2, -1])).toBe(2); | ||
}); | ||
}); |
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,11 @@ | ||
export function sumOfDifferences(arr: number[]): number { | ||
arr.sort((a, b) => b - a); | ||
|
||
let sum = 0; | ||
|
||
for (let i = 0; i < arr.length - 1; ++i) { | ||
sum += arr[i] - arr[i + 1]; | ||
} | ||
|
||
return sum; | ||
} |
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
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