From time to time I try to do challenge myself with some Kata exercise. I found them very helpful in improving my development skills, codding efficiency, and most important algorithmic thinking.
They reinforce best practices and quick thinking. IMHO regular practice builds some kind of memory patterns and techniques, making coding faster and more intuitive.
All my challenges I find in the codewars.com page – I highly recommend this page.
This challenge does not need actually much of the introduction, let the examples show what I mean:
for this “RqaEzty”, we should get this one: “R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy”.
for “RqaEzty”, we should get this one: “R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy”
for “cwAt”, we should get this one: “C-Ww-Aaa-Tttt”
and so on…
So as the title says… mumbling.
FYI the parameter of test method is a string which includes only letters from a..z and A..Z.
Here is my solution:
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Accumul {
public static String accum(final String text) {
if (text == null) {
throw new IllegalArgumentException();
}
return IntStream.range(0, text.length())
.mapToObj(i -> singleCharToWord(text.charAt(i), i))
.collect(Collectors.joining("-"));
}
private static String singleCharToWord(final Character charAt, final int index) {
return charAt.toString().toUpperCase() + IntStream.range(1, index + 1)
.mapToObj(i -> String.valueOf(charAt).toLowerCase())
.collect(Collectors.joining());
}
}
and here are some simple tests for that
public class AccumulTest {
@Test
public void testWhenGivenNullAsInput() {
assertThrows(IllegalArgumentException.class, () -> {
Accumul.accum(null);
});
}
@Test
public void test() {
assertEquals("", Accumul.accum(""));
assertEquals("A", Accumul.accum("a"));
assertEquals("A-Bb", Accumul.accum("ab"));
assertEquals("Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu", Accumul.accum("ZpglnRxqenU"));
assertEquals("N-Yy-Fff-Ffff-Sssss-Gggggg-Eeeeeee-Yyyyyyyy-Yyyyyyyyy-Llllllllll-Bbbbbbbbbbb", Accumul.accum("NyffsGeyylB"));
assertEquals("M-Jj-Ttt-Kkkk-Uuuuu-Bbbbbb-Ooooooo-Vvvvvvvv-Qqqqqqqqq-Rrrrrrrrrr-Uuuuuuuuuuu", Accumul.accum("MjtkuBovqrU"));
assertEquals("E-Vv-Iii-Dddd-Jjjjj-Uuuuuu-Nnnnnnn-Oooooooo-Kkkkkkkkk-Mmmmmmmmmm-Mmmmmmmmmmm", Accumul.accum("EvidjUnokmM"));
assertEquals("H-Bb-Iii-Dddd-Eeeee-Vvvvvv-Bbbbbbb-Xxxxxxxx-Nnnnnnnnn-Cccccccccc-Ccccccccccc", Accumul.accum("HbideVbxncC"));
}
}
That would be all, thanks for your time, and till the next time :)
Top comments (0)