From e4219d97b3dc3c5dea297c4b69873683c41e31e7 Mon Sep 17 00:00:00 2001 From: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com> Date: Wed, 8 Jan 2025 04:26:02 +0000 Subject: [PATCH 1/4] docs: kata description --- kata/6-kyu/simple-sum-of-pairs/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 kata/6-kyu/simple-sum-of-pairs/README.md 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..b69c43aad --- /dev/null +++ b/kata/6-kyu/simple-sum-of-pairs/README.md @@ -0,0 +1,19 @@ +# [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: +```Pearl +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. + +Good luck! From 72d49989ebeb06979784195216fb72a23478ae23 Mon Sep 17 00:00:00 2001 From: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com> Date: Wed, 8 Jan 2025 04:30:25 +0000 Subject: [PATCH 2/4] docs: sync progress --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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. From a4979e6db0feab1a4a7696eb16c9c2ed4ef923fe Mon Sep 17 00:00:00 2001 From: "Capt. Cutlass" <5120290+ParanoidUser@users.noreply.github.com> Date: Tue, 7 Jan 2025 23:55:36 -0500 Subject: [PATCH 3/4] feat: kata/simple-sum-of-pairs --- kata/6-kyu/index.md | 1 + kata/6-kyu/simple-sum-of-pairs/README.md | 11 +++++----- .../simple-sum-of-pairs/main/Solution.java | 9 ++++++++ .../test/SolutionTest.java | 21 +++++++++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 kata/6-kyu/simple-sum-of-pairs/main/Solution.java create mode 100644 kata/6-kyu/simple-sum-of-pairs/test/SolutionTest.java 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 index b69c43aad..ec4855cbb 100644 --- a/kata/6-kyu/simple-sum-of-pairs/README.md +++ b/kata/6-kyu/simple-sum-of-pairs/README.md @@ -1,19 +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: -```Pearl + +``` 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). + +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. +`n` will not exceed `10e10`. -Good luck! +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..82efd2dea --- /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, (n + "").length() - 1.) - 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 From cfbf20209321da75917c480fc931850ab7e5d7c1 Mon Sep 17 00:00:00 2001 From: "Capt. Cutlass" <5120290+ParanoidUser@users.noreply.github.com> Date: Tue, 7 Jan 2025 23:57:30 -0500 Subject: [PATCH 4/4] feat: kata/simple-sum-of-pairs --- kata/6-kyu/simple-sum-of-pairs/main/Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kata/6-kyu/simple-sum-of-pairs/main/Solution.java b/kata/6-kyu/simple-sum-of-pairs/main/Solution.java index 82efd2dea..e4d4104d9 100644 --- a/kata/6-kyu/simple-sum-of-pairs/main/Solution.java +++ b/kata/6-kyu/simple-sum-of-pairs/main/Solution.java @@ -3,7 +3,7 @@ interface Solution { static int solve(long n) { LongToIntFunction sum = l -> (l + "").chars().map(c -> c - 48).sum(); - long a = (long) Math.pow(10, (n + "").length() - 1.) - 1; + 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