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
20 changes: 20 additions & 0 deletions kata/7-kyu/get-the-length-of-a-string/README.md
Original file line number Diff line number Diff line change
@@ -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
```
5 changes: 5 additions & 0 deletions kata/7-kyu/get-the-length-of-a-string/main/Kata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface Kata {
static int getRealLength(String str) {
return str.codePointCount(0, str.length());
}
}
21 changes: 21 additions & 0 deletions kata/7-kyu/get-the-length-of-a-string/test/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
1 change: 1 addition & 0 deletions kata/7-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down