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 @@ -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")
Expand Down
11 changes: 11 additions & 0 deletions kata/6-kyu/sentence-calculator/README.md
Original file line number Diff line number Diff line change
@@ -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*
5 changes: 5 additions & 0 deletions kata/6-kyu/sentence-calculator/main/SentenceCalculator.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
19 changes: 19 additions & 0 deletions kata/6-kyu/sentence-calculator/test/SampleTests.java
Original file line number Diff line number Diff line change
@@ -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));
}
}