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..5bb15e5e --- /dev/null +++ b/kata/7-kyu/get-the-length-of-a-string/README.md @@ -0,0 +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. + +For example: + +The code point of the emoji `πŸ™‰` (`U+1F649`, *Hear-No-Evil Monkey*) is `0x1F649`. + +```java +"πŸ™‰".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 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")