diff --git a/kata/6-kyu/index.md b/kata/6-kyu/index.md index 22303297..cc110361 100644 --- a/kata/6-kyu/index.md +++ b/kata/6-kyu/index.md @@ -404,6 +404,7 @@ - [The Walker](the-walker "5b40b666dfb4291ad9000049") - [Throw without throwing](throw-without-throwing "5943db60800cebe12000003d") - [Tic-Tac-Toe-like table Generator](tic-tac-toe-like-table-generator "5b817c2a0ce070ace8002be0") +- [Ticker](ticker "5a959662373c2e761d000183") - [Time Math](time-math "5aceae374d9fd1266f0000f0") - [Tortoise racing](tortoise-racing "55e2adece53b4cdcb900006c") - [Traffic Lights - one car](traffic-lights-one-car "5d0ae91acac0a50232e8a547") diff --git a/kata/6-kyu/ticker/README.md b/kata/6-kyu/ticker/README.md new file mode 100644 index 00000000..1a35e70a --- /dev/null +++ b/kata/6-kyu/ticker/README.md @@ -0,0 +1,29 @@ +# [Ticker](https://www.codewars.com/kata/ticker "https://www.codewars.com/kata/5a959662373c2e761d000183") + +While using public transport we could see simple displays with a ticker. It often provides information about next stations and expected +arrival time. + +Let's implement a function which will return a chunk of text to be displayed on a display of fixed width. +The function should take `text` to display, `width` of the display and `tick` as a step of the ticker. The function will be called many +times with `tick` parameter constantly incrementing. + +At very beginning the display is empty. At first step only one character should be displayed in the most right position and so on. When the +message is displayed, it should disappear char by char to left position and return to blank state of the display. After that cycle should +start again. + +For example + +```java +ticker("Hello world!", 10, 4); // " Hell" +``` + +We could expect that our function will be called from simple script like this one + +```java +void demo(String text, int width) { + for (int tick = 0; isRunning(); tick++) { + display.setText(ticker(text, width, tick)); + Thread.sleep(100L); + } +} +``` \ No newline at end of file diff --git a/kata/6-kyu/ticker/main/Kata.java b/kata/6-kyu/ticker/main/Kata.java new file mode 100644 index 00000000..649bd71f --- /dev/null +++ b/kata/6-kyu/ticker/main/Kata.java @@ -0,0 +1,6 @@ +interface Kata { + static String ticker(String text, int width, int tick) { + tick %= (width + text.length()); + return (" ".repeat(width) + text + " ".repeat(width)).substring(tick, tick + width); + } +} \ No newline at end of file diff --git a/kata/6-kyu/ticker/test/SolutionTest.java b/kata/6-kyu/ticker/test/SolutionTest.java new file mode 100644 index 00000000..1d2d83d9 --- /dev/null +++ b/kata/6-kyu/ticker/test/SolutionTest.java @@ -0,0 +1,18 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class SolutionTest { + @ParameterizedTest + @CsvSource(textBlock = """ + 10, 0, ' ' + 10, 5, ' Beaut' + 10, 30, 'than ugly.' + 10, 39, '. ' + 10, 41, ' B' + """) + void sample(int width, int tick, String display) { + assertEquals(display, Kata.ticker("Beautiful is better than ugly.", width, tick)); + } +} \ No newline at end of file