From a2f3b2c626487eb941f3a5bf4a43bd17ad0ba778 Mon Sep 17 00:00:00 2001 From: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com> Date: Fri, 8 Aug 2025 16:24:17 +0000 Subject: [PATCH 1/2] docs: kata description --- .../get-the-length-of-a-string/README.md | 27 +++++++++++++++++++ kata/7-kyu/index.md | 1 + 2 files changed, 28 insertions(+) create mode 100644 kata/7-kyu/get-the-length-of-a-string/README.md diff --git a/kata/7-kyu/get-the-length-of-a-string/README.md b/kata/7-kyu/get-the-length-of-a-string/README.md new file mode 100644 index 00000000..733f8e34 --- /dev/null +++ b/kata/7-kyu/get-the-length-of-a-string/README.md @@ -0,0 +1,27 @@ +# [Get the π‘Ÿπ‘’π‘Žπ‘™ length of a string](https://www.codewars.com/kata/get-the-length-of-a-string "https://www.codewars.com/kata/599c4b69eb8e49effa000079") + +In languages that use [UTF-16 encoding](https://en.wikipedia.org/wiki/UTF-16) for strings (JavaScript, JVM languages like Java, .NET languages like C#...), if the code point of a character is larger than `0xFFFF`, it will be treated as two code units. + +For example: + +The code point of the emoji `πŸ™‰` (`U+1F649`, *Hear-No-Evil Monkey*) is `0x1F649`. + +```javascript +'πŸ™‰'.length; // 2 +``` + +```java +"πŸ™‰".length(); // 2 +``` + +```csharp +"πŸ™‰".Length; // 2 +``` + +Write a function that returns the *real* length of a string. + +``` +"abcd" --> 4 +"πŸ™‰" --> 1 +"πŸ˜ΈπŸ¦ŒπŸš€" --> 3 +``` diff --git a/kata/7-kyu/index.md b/kata/7-kyu/index.md index 89292cfb..11fdcc5f 100644 --- a/kata/7-kyu/index.md +++ b/kata/7-kyu/index.md @@ -231,6 +231,7 @@ - [GCD sum](gcd-sum "5dd259444228280032b1ed2a") - [Geometry Basics: Dot Product in 3D](geometry-basics-dot-product-in-3d "58e3ea29a33b52c1dc0000c0") - [Geometry Basics: Triangle Perimeter in 2D](geometry-basics-triangle-perimeter-in-2d "58e3e62f20617b6d7700120a") +- [Get the π‘Ÿπ‘’π‘Žπ‘™ length of a string](get-the-length-of-a-string "599c4b69eb8e49effa000079") - [Get the Middle Character](get-the-middle-character "56747fd5cb988479af000028") - [Ghostbusters (whitespace removal)](ghostbusters-whitespace-removal "5668e3800636a6cd6a000018") - [Go: Stone Scoring](go-stone-scoring "5fdb2ef8656423001c00e648") From a51f845e6a9e26e1e77ae09fae8bcc17970f335e Mon Sep 17 00:00:00 2001 From: "Capt. Cutlass" <5120290+ParanoidUser@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:13:44 -0400 Subject: [PATCH 2/2] feat: kata/get-the-length-of-a-string --- .../get-the-length-of-a-string/README.md | 13 +++--------- .../get-the-length-of-a-string/main/Kata.java | 5 +++++ .../test/SolutionTest.java | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 kata/7-kyu/get-the-length-of-a-string/main/Kata.java create mode 100644 kata/7-kyu/get-the-length-of-a-string/test/SolutionTest.java diff --git a/kata/7-kyu/get-the-length-of-a-string/README.md b/kata/7-kyu/get-the-length-of-a-string/README.md index 733f8e34..5bb15e5e 100644 --- a/kata/7-kyu/get-the-length-of-a-string/README.md +++ b/kata/7-kyu/get-the-length-of-a-string/README.md @@ -1,27 +1,20 @@ # [Get the π‘Ÿπ‘’π‘Žπ‘™ length of a string](https://www.codewars.com/kata/get-the-length-of-a-string "https://www.codewars.com/kata/599c4b69eb8e49effa000079") -In languages that use [UTF-16 encoding](https://en.wikipedia.org/wiki/UTF-16) for strings (JavaScript, JVM languages like Java, .NET languages like C#...), if the code point of a character is larger than `0xFFFF`, it will be treated as two code units. +In languages that use [UTF-16 encoding](https://en.wikipedia.org/wiki/UTF-16) for strings (JavaScript, JVM languages like Java, .NET +languages like C#...), if the code point of a character is larger than `0xFFFF`, it will be treated as two code units. For example: The code point of the emoji `πŸ™‰` (`U+1F649`, *Hear-No-Evil Monkey*) is `0x1F649`. -```javascript -'πŸ™‰'.length; // 2 -``` - ```java "πŸ™‰".length(); // 2 ``` -```csharp -"πŸ™‰".Length; // 2 -``` - Write a function that returns the *real* length of a string. ``` "abcd" --> 4 "πŸ™‰" --> 1 "πŸ˜ΈπŸ¦ŒπŸš€" --> 3 -``` +``` \ No newline at end of file diff --git a/kata/7-kyu/get-the-length-of-a-string/main/Kata.java b/kata/7-kyu/get-the-length-of-a-string/main/Kata.java new file mode 100644 index 00000000..d4ce55c3 --- /dev/null +++ b/kata/7-kyu/get-the-length-of-a-string/main/Kata.java @@ -0,0 +1,5 @@ +interface Kata { + static int getRealLength(String str) { + return str.codePointCount(0, str.length()); + } +} \ No newline at end of file diff --git a/kata/7-kyu/get-the-length-of-a-string/test/SolutionTest.java b/kata/7-kyu/get-the-length-of-a-string/test/SolutionTest.java new file mode 100644 index 00000000..855a51bc --- /dev/null +++ b/kata/7-kyu/get-the-length-of-a-string/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 + abcd, 4 + δΈ­ε›½, 2 + π“ͺ𝓫𝓬𝓭, 4 + 𨭎𩷢, 2 + πŸ˜ΈπŸ¦ŒπŸš€, 3 + ↓→↑←, 4 + '\nabc\ndef\n', 9 + """) + void sample(String str, int expected) { + assertEquals(expected, Kata.getRealLength(str)); + } +} \ No newline at end of file