Skip to content

Commit 94a1cbd

Browse files
* docs: kata description * feat: kata/get-the-length-of-a-string --------- Co-authored-by: ParanoidUser <[email protected]>
1 parent 1566e0e commit 94a1cbd

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# [Get the π‘Ÿπ‘’π‘Žπ‘™ length of a string](https://www.codewars.com/kata/get-the-length-of-a-string "https://www.codewars.com/kata/599c4b69eb8e49effa000079")
2+
3+
In languages that use [UTF-16 encoding](https://en.wikipedia.org/wiki/UTF-16) for strings (JavaScript, JVM languages like Java, .NET
4+
languages like C#...), if the code point of a character is larger than `0xFFFF`, it will be treated as two code units.
5+
6+
For example:
7+
8+
The code point of the emoji `πŸ™‰` (`U+1F649`, *Hear-No-Evil Monkey*) is `0x1F649`.
9+
10+
```java
11+
"πŸ™‰".length(); // 2
12+
```
13+
14+
Write a function that returns the *real* length of a string.
15+
16+
```
17+
"abcd" --> 4
18+
"πŸ™‰" --> 1
19+
"πŸ˜ΈπŸ¦ŒπŸš€" --> 3
20+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface Kata {
2+
static int getRealLength(String str) {
3+
return str.codePointCount(0, str.length());
4+
}
5+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
class SolutionTest {
7+
@ParameterizedTest
8+
@CsvSource(textBlock = """
9+
'', 0
10+
abcd, 4
11+
δΈ­ε›½, 2
12+
π“ͺ𝓫𝓬𝓭, 4
13+
𨭎𩷢, 2
14+
πŸ˜ΈπŸ¦ŒπŸš€, 3
15+
↓→↑←, 4
16+
'\nabc\ndef\n', 9
17+
""")
18+
void sample(String str, int expected) {
19+
assertEquals(expected, Kata.getRealLength(str));
20+
}
21+
}

β€Žkata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@
231231
- [GCD sum](gcd-sum "5dd259444228280032b1ed2a")
232232
- [Geometry Basics: Dot Product in 3D](geometry-basics-dot-product-in-3d "58e3ea29a33b52c1dc0000c0")
233233
- [Geometry Basics: Triangle Perimeter in 2D](geometry-basics-triangle-perimeter-in-2d "58e3e62f20617b6d7700120a")
234+
- [Get the π‘Ÿπ‘’π‘Žπ‘™ length of a string](get-the-length-of-a-string "599c4b69eb8e49effa000079")
234235
- [Get the Middle Character](get-the-middle-character "56747fd5cb988479af000028")
235236
- [Ghostbusters (whitespace removal)](ghostbusters-whitespace-removal "5668e3800636a6cd6a000018")
236237
- [Go: Stone Scoring](go-stone-scoring "5fdb2ef8656423001c00e648")

0 commit comments

Comments
Β (0)