diff --git a/docs/README.md b/docs/README.md index f54f6c6ec..9a7d600c2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,7 +1,7 @@ # Codewars Handbook ☕️🚀 [![Views statistics +1 👀](https://img.shields.io/badge/dynamic/xml?color=success&label=views&query=//*[name()=%27text%27][3]&url=https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FParanoidUser%2Fcodewars-handbook)](https://hits.seeyoufarm.com/api/count/graph/dailyhits.svg?url=https://github.com/ParanoidUser/codewars-handbook) -[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-68.9%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed) +[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-68.4%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed) [![CI pipeline 🛠](https://img.shields.io/github/actions/workflow/status/ParanoidUser/codewars-handbook/build.yml?branch=main)](https://github.com/ParanoidUser/codewars-handbook/actions/workflows/build.yml) [![Quality gate 🔎](https://img.shields.io/sonar/alert_status/codewars-handbook?server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/dashboard?id=codewars-handbook) [![Let's have a chat! 📞](https://img.shields.io/gitter/room/ParanoidUser/codewars-handbook?color=49c39e)](https://gitter.im/ParanoidUser/codewars-handbook) @@ -25,7 +25,7 @@ slug. | [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) | |:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:| -| 0 | 1 | 2 | 26 | 48 | 433 | 590 | 219 | 54 | 82 | +| 0 | 1 | 2 | 26 | 48 | 433 | 590 | 220 | 54 | 82 | **Note:** The source code is written in Java 17 and may use language features that are incompatible with Java 8, 11. diff --git a/kata/8-kyu/closest-elevator/README.md b/kata/8-kyu/closest-elevator/README.md new file mode 100644 index 000000000..d845741b7 --- /dev/null +++ b/kata/8-kyu/closest-elevator/README.md @@ -0,0 +1,25 @@ +# [Closest elevator](https://www.codewars.com/kata/closest-elevator "https://www.codewars.com/kata/5c374b346a5d0f77af500a5a") + +Given 2 elevators (named "left" and "right") in a building with 3 floors (numbered `0` to `2`), write a function accepting 3 arguments (in +order): + +- `left` - The current floor of the left elevator +- `right` - The current floor of the right elevator +- `call` - The floor that called an elevator + +It should return the name of the elevator closest to the called floor (`"left"`/`"right"`). + +In the case where both elevators are equally distant from the called floor, choose the elevator to the right. + +You can assume that the inputs will always be valid integers between 0-2. + +Examples: + +``` +left right call result + 0 1 0 "left" + 0 1 1 "right" + 0 1 2 "right" + 0 0 0 "right" + 0 2 1 "right" +``` \ No newline at end of file diff --git a/kata/8-kyu/closest-elevator/main/Elevator.java b/kata/8-kyu/closest-elevator/main/Elevator.java new file mode 100644 index 000000000..3844afed3 --- /dev/null +++ b/kata/8-kyu/closest-elevator/main/Elevator.java @@ -0,0 +1,5 @@ +interface Elevator { + static String call(int left, int right, int call) { + return Math.abs(left - call) < Math.abs(right - call) ? "left" : "right"; + } +} \ No newline at end of file diff --git a/kata/8-kyu/closest-elevator/test/TestElevator.java b/kata/8-kyu/closest-elevator/test/TestElevator.java new file mode 100644 index 000000000..2bcff0dc8 --- /dev/null +++ b/kata/8-kyu/closest-elevator/test/TestElevator.java @@ -0,0 +1,16 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class TestElevator { + @ParameterizedTest + @CsvSource(textBlock = """ + 0, 0, 0, right + 0, 1, 0, left + 0, 2, 0, left + """) + void sample(int left, int right, int call, String expected) { + assertEquals(expected, Elevator.call(left, right, call)); + } +} \ No newline at end of file diff --git a/kata/8-kyu/index.md b/kata/8-kyu/index.md index 4ef9a95a5..9e04facce 100644 --- a/kata/8-kyu/index.md +++ b/kata/8-kyu/index.md @@ -34,6 +34,7 @@ - [Century From Year](century-from-year) - [Circles in Polygons](circles-in-polygons) - [Classic Hello World](classic-hello-world) +- [Closest elevator](closest-elevator) - [Collatz Conjecture (3n+1)](collatz-conjecture-3n-plus-1) - [Collinearity](collinearity) - [Color Ghost](color-ghost)