Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kata/6-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
29 changes: 29 additions & 0 deletions kata/6-kyu/ticker/README.md
Original file line number Diff line number Diff line change
@@ -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);
}
}
```
6 changes: 6 additions & 0 deletions kata/6-kyu/ticker/main/Kata.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 18 additions & 0 deletions kata/6-kyu/ticker/test/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}