Skip to content

Commit 6313e70

Browse files
authored
reverse-string: encourage grapheme awareness (JuliaLang#261)
Closes JuliaLang#238
1 parent 87e24ce commit 6313e70

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

exercises/reverse-string/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ For example:
66
input: "cool"
77
output: "looc"
88

9+
## Bonus
10+
11+
Preserve grapheme clusters, i.e.
12+
13+
```julia
14+
myreverse("hi 👋🏾") == "👋🏾 ih"
15+
myreverse("as⃝df̅") == "f̅ds⃝a"
16+
```
17+
18+
You will probably find the `Unicode` stdlib useful for this bonus task.
19+
20+
To enable the graphemes test, add `const TEST_GRAPHEMES = true` to the global scope of your file.
21+
922
## Source
1023

1124
Introductory challenge to reverse an input string [https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb](https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb)

exercises/reverse-string/example.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
using Unicode: graphemes
2+
3+
const TEST_GRAPHEMES = true
4+
15
function myreverse(phrase::String)
2-
chars = Char[]
3-
for c in phrase
4-
push!(chars, c)
5-
end
6-
join(chars[end:-1:1])
6+
join(reverse(collect(graphemes(phrase))))
77
end

exercises/reverse-string/reverse-string.jl

Lines changed: 0 additions & 3 deletions
This file was deleted.

exercises/reverse-string/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,14 @@ end
3232
@testset "reversing a string twice" begin
3333
@test myreverse(myreverse("gift")) == "gift"
3434
end
35+
36+
@testset "emoji" begin
37+
@test myreverse("hi 🐱") == "🐱 ih"
38+
end
39+
40+
if @isdefined(TEST_GRAPHEMES)
41+
@eval @testset "graphemes" begin
42+
@test myreverse("as⃝df̅") == "f̅ds⃝a"
43+
@test myreverse("hi 👋🏾") == "👋🏾 ih"
44+
end
45+
end

0 commit comments

Comments
 (0)