Skip to content

Commit b3ae515

Browse files
* docs: kata description * feat: kata/elevator-distance --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
1 parent 343f7d9 commit b3ae515

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# [Elevator Distance](https://www.codewars.com/kata/elevator-distance "https://www.codewars.com/kata/59f061773e532d0c87000d16")
2+
3+
Imagine you start on the 5th floor of a building, then travel down to the 2nd floor, then back up to the 8th floor. You have travelled a
4+
total of 3 + 6 = 9 floors of distance.
5+
6+
Given an array representing a series of floors you must reach by elevator, return an integer representing the total distance travelled for
7+
visiting each floor in the array in order.
8+
9+
```
10+
// simple examples
11+
[5,2,8] => 9
12+
[1,2,3] => 2
13+
[7,1,7,1] => 18
14+
15+
// if two consecutive floors are the same,
16+
//distance travelled between them is 0
17+
[3,3] => 0
18+
```
19+
20+
Array will always contain at least 2 floors. Random tests will contain 2-20 elements in array, and floor values between 0 and 30.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import static java.util.stream.IntStream.range;
2+
3+
interface Kata {
4+
static int elevatorDistance(int[] arr) {
5+
return range(0, arr.length - 1).reduce(0, (s, i) -> s + Math.abs(arr[i] - arr[i + 1]));
6+
}
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
import static org.junit.jupiter.params.provider.Arguments.arguments;
3+
4+
import java.util.stream.Stream;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
class ElevatorDistanceTest {
10+
private static Stream<Arguments> testData() {
11+
return Stream.of(
12+
arguments(new int[]{5, 2, 8}, 9),
13+
arguments(new int[]{1, 2, 3}, 2),
14+
arguments(new int[]{7, 1, 7, 1}, 18)
15+
);
16+
}
17+
18+
@ParameterizedTest
19+
@MethodSource("testData")
20+
void sample(int[] arr, int expected) {
21+
assertEquals(expected, Kata.elevatorDistance(arr));
22+
}
23+
}

kata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
- [EAN Validation](ean-validation "55563df50dda59adf900004d")
158158
- [Easy Line](easy-line "56e7d40129035aed6c000632")
159159
- [Easy wallpaper](easy-wallpaper "567501aec64b81e252000003")
160+
- [Elevator Distance](elevator-distance "59f061773e532d0c87000d16")
160161
- [Eliminate the intruders! Bit manipulation](eliminate-the-intruders-bit-manipulation "5a0d38c9697598b67a000041")
161162
- [Email Address Obfuscator](email-address-obfuscator "562d8d4c434582007300004e")
162163
- [E.S.P. Cards](esp-cards "596ef174e4cab6813600004d")

0 commit comments

Comments
 (0)