Skip to content

Commit e07861b

Browse files
* docs: kata description * feat: kata/ticker --------- Co-authored-by: ParanoidUser <[email protected]>
1 parent 7b35f76 commit e07861b

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

kata/6-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@
404404
- [The Walker](the-walker "5b40b666dfb4291ad9000049")
405405
- [Throw without throwing](throw-without-throwing "5943db60800cebe12000003d")
406406
- [Tic-Tac-Toe-like table Generator](tic-tac-toe-like-table-generator "5b817c2a0ce070ace8002be0")
407+
- [Ticker](ticker "5a959662373c2e761d000183")
407408
- [Time Math](time-math "5aceae374d9fd1266f0000f0")
408409
- [Tortoise racing](tortoise-racing "55e2adece53b4cdcb900006c")
409410
- [Traffic Lights - one car](traffic-lights-one-car "5d0ae91acac0a50232e8a547")

kata/6-kyu/ticker/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# [Ticker](https://www.codewars.com/kata/ticker "https://www.codewars.com/kata/5a959662373c2e761d000183")
2+
3+
While using public transport we could see simple displays with a ticker. It often provides information about next stations and expected
4+
arrival time.
5+
6+
Let's implement a function which will return a chunk of text to be displayed on a display of fixed width.
7+
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
8+
times with `tick` parameter constantly incrementing.
9+
10+
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
11+
message is displayed, it should disappear char by char to left position and return to blank state of the display. After that cycle should
12+
start again.
13+
14+
For example
15+
16+
```java
17+
ticker("Hello world!", 10, 4); // " Hell"
18+
```
19+
20+
We could expect that our function will be called from simple script like this one
21+
22+
```java
23+
void demo(String text, int width) {
24+
for (int tick = 0; isRunning(); tick++) {
25+
display.setText(ticker(text, width, tick));
26+
Thread.sleep(100L);
27+
}
28+
}
29+
```

kata/6-kyu/ticker/main/Kata.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface Kata {
2+
static String ticker(String text, int width, int tick) {
3+
tick %= (width + text.length());
4+
return (" ".repeat(width) + text + " ".repeat(width)).substring(tick, tick + width);
5+
}
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
class SolutionTest {
7+
@ParameterizedTest
8+
@CsvSource(textBlock = """
9+
10, 0, ' '
10+
10, 5, ' Beaut'
11+
10, 30, 'than ugly.'
12+
10, 39, '. '
13+
10, 41, ' B'
14+
""")
15+
void sample(int width, int tick, String display) {
16+
assertEquals(display, Kata.ticker("Beautiful is better than ugly.", width, tick));
17+
}
18+
}

0 commit comments

Comments
 (0)