Skip to content

Commit 06ab56c

Browse files
committed
1 parent a2e7553 commit 06ab56c

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

kata/7-kyu/elevator-distance/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# [Elevator Distance](https://www.codewars.com/kata/elevator-distance "https://www.codewars.com/kata/59f061773e532d0c87000d16")
22

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 total of 3 + 6 = 9 floors of distance.
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.
45

5-
Given an array representing a series of floors you must reach by elevator, return an integer representing the total distance travelled for visiting each floor in the array in order.
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.
68

79
```
810
// simple examples
@@ -15,4 +17,4 @@ Given an array representing a series of floors you must reach by elevator, retur
1517
[3,3] => 0
1618
```
1719

18-
Array will always contain at least 2 floors. Random tests will contain 2-20 elements in array, and floor values between 0 and 30.
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.
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+
}
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+
}

0 commit comments

Comments
 (0)