diff --git a/kata/6-kyu/index.md b/kata/6-kyu/index.md index f8d4fe45..c4150b9e 100644 --- a/kata/6-kyu/index.md +++ b/kata/6-kyu/index.md @@ -308,6 +308,7 @@ - [Row of the odd triangle](row-of-the-odd-triangle "5d5a7525207a674b71aa25b5") - [Salesman's Travel](salesmans-travel "56af1a20509ce5b9b000001e") - [Same matrix (2 * 2)](same-matrix-2-star-2 "635fc0497dadea0030cb7936") +- [Sentence Calculator](sentence-calculator "5970fce80ed776b94000008b") - [Separate The Wheat From The Chaff](separate-the-wheat-from-the-chaff "5bdcd20478d24e664d00002c") - [Sequence convergence](sequence-convergence "59971e64bfccc70748000068") - [Sequences and Series](sequences-and-series "5254bd1357d59fbbe90001ec") diff --git a/kata/6-kyu/sentence-calculator/README.md b/kata/6-kyu/sentence-calculator/README.md new file mode 100644 index 00000000..324183eb --- /dev/null +++ b/kata/6-kyu/sentence-calculator/README.md @@ -0,0 +1,11 @@ +# [Sentence Calculator](https://www.codewars.com/kata/sentence-calculator "https://www.codewars.com/kata/5970fce80ed776b94000008b") + +Calculate the total score (sum of the individual character scores) of a sentence given the following score rules for each allowed group of +characters: + +1. Lower case [a-z]: 'a'=1, 'b'=2, 'c'=3, ..., 'z'=26 +2. Upper case [A-Z]: 'A'=2, 'B'=4, 'C'=6, ..., 'Z'=52 +3. Digits [0-9] their numeric value: '0'=0, '1'=1, '2'=2, ..., '9'=9 +4. Other characters: 0 value + +*Note: input will always be a string* \ No newline at end of file diff --git a/kata/6-kyu/sentence-calculator/main/SentenceCalculator.java b/kata/6-kyu/sentence-calculator/main/SentenceCalculator.java new file mode 100644 index 00000000..bbd74079 --- /dev/null +++ b/kata/6-kyu/sentence-calculator/main/SentenceCalculator.java @@ -0,0 +1,5 @@ +interface SentenceCalculator { + static int lettersToNumbers(String s) { + return s.replaceAll("\\W", "").chars().map(c -> c < 58 ? c - 48 : c < 91 ? 2 * (c - 64) : c - 96).sum(); + } +} \ No newline at end of file diff --git a/kata/6-kyu/sentence-calculator/test/SampleTests.java b/kata/6-kyu/sentence-calculator/test/SampleTests.java new file mode 100644 index 00000000..80f49cce --- /dev/null +++ b/kata/6-kyu/sentence-calculator/test/SampleTests.java @@ -0,0 +1,19 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class SampleTests { + @ParameterizedTest + @CsvSource(delimiter = '|', textBlock = """ + Give me 5! | 73 + Give me five! | 110 + oops, i did it again! | 152 + I Love You | 170 + ILoveYou | 170 + ARE YOU HUNGRY? | 356 + """) + void sample(String input, int expected) { + assertEquals(expected, SentenceCalculator.lettersToNumbers(input)); + } +} \ No newline at end of file