diff --git a/docs/README.md b/docs/README.md index 29f72dfc7..ec021c1a5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,7 +25,7 @@ slug. | [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) | |:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:| -| 0 | 1 | 2 | 26 | 48 | 441 | 605 | 227 | 56 | 82 | +| 0 | 1 | 2 | 26 | 48 | 442 | 605 | 227 | 56 | 82 | **Note:** The source code is written in Java 17 and may use language features that are incompatible with Java 8, 11. diff --git a/kata/6-kyu/index.md b/kata/6-kyu/index.md index f1a9a414a..d9c829dff 100644 --- a/kata/6-kyu/index.md +++ b/kata/6-kyu/index.md @@ -352,6 +352,7 @@ - [Simple ROT13.5 cypher](simple-rot13-dot-5-cypher) - [Simple square numbers](simple-square-numbers) - [Simple string indices](simple-string-indices) +- [Simple sum of pairs](simple-sum-of-pairs) - [Simple time difference](simple-time-difference) - [Simpson's Rule - Approximate Integration](simpsons-rule-approximate-integration) - [Single character palindromes](single-character-palindromes) diff --git a/kata/6-kyu/simple-sum-of-pairs/README.md b/kata/6-kyu/simple-sum-of-pairs/README.md new file mode 100644 index 000000000..ec4855cbb --- /dev/null +++ b/kata/6-kyu/simple-sum-of-pairs/README.md @@ -0,0 +1,20 @@ +# [Simple sum of pairs](https://www.codewars.com/kata/simple-sum-of-pairs "https://www.codewars.com/kata/5bc027fccd4ec86c840000b7") + +Given an integer `n`, find two integers `a` and `b` such that: + +``` +A) a >= 0 and b >= 0 +B) a + b = n +C) DigitSum(a) + Digitsum(b) is maximum of all possibilities. +``` + +You will return the digitSum(a) + digitsum(b). + +``` +For example: +solve(29) = 11. If we take 15 + 14 = 29 and digitSum = 1 + 5 + 1 + 4 = 11. There is no larger outcome. +``` + +`n` will not exceed `10e10`. + +More examples in test cases. \ No newline at end of file diff --git a/kata/6-kyu/simple-sum-of-pairs/main/Solution.java b/kata/6-kyu/simple-sum-of-pairs/main/Solution.java new file mode 100644 index 000000000..e4d4104d9 --- /dev/null +++ b/kata/6-kyu/simple-sum-of-pairs/main/Solution.java @@ -0,0 +1,9 @@ +import java.util.function.LongToIntFunction; + +interface Solution { + static int solve(long n) { + LongToIntFunction sum = l -> (l + "").chars().map(c -> c - 48).sum(); + long a = (long) Math.pow(10, Math.floor(Math.log10(n))) - 1; + return (int) (n > 1 ? sum.applyAsInt(a) + sum.applyAsInt(n - a) : n); + } +} \ No newline at end of file diff --git a/kata/6-kyu/simple-sum-of-pairs/test/SolutionTest.java b/kata/6-kyu/simple-sum-of-pairs/test/SolutionTest.java new file mode 100644 index 000000000..73d81373b --- /dev/null +++ b/kata/6-kyu/simple-sum-of-pairs/test/SolutionTest.java @@ -0,0 +1,21 @@ +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 = """ + 0, 0 + 1, 1 + 18, 18 + 29, 11 + 1140, 33 + 50000000, 68 + 458453062, 109 + 8130375074, 119 + """) + void sample(long n, int sum) { + assertEquals(sum, Solution.solve(n)); + } +} \ No newline at end of file