diff --git a/exercises/reverse-string/README.md b/exercises/reverse-string/README.md index c348730f..c766c765 100644 --- a/exercises/reverse-string/README.md +++ b/exercises/reverse-string/README.md @@ -6,6 +6,19 @@ For example: input: "cool" output: "looc" +## Bonus + +Preserve grapheme clusters, i.e. + +```julia +myreverse("hi 👋🏾") == "👋🏾 ih" +myreverse("as⃝df̅") == "f̅ds⃝a" +``` + +You will probably find the `Unicode` stdlib useful for this bonus task. + +To enable the graphemes test, add `const TEST_GRAPHEMES = true` to the global scope of your file. + ## Source 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) diff --git a/exercises/reverse-string/example.jl b/exercises/reverse-string/example.jl index 9ea3e30b..3413e3c6 100644 --- a/exercises/reverse-string/example.jl +++ b/exercises/reverse-string/example.jl @@ -1,7 +1,7 @@ +using Unicode: graphemes + +const TEST_GRAPHEMES = true + function myreverse(phrase::String) - chars = Char[] - for c in phrase - push!(chars, c) - end - join(chars[end:-1:1]) + join(reverse(collect(graphemes(phrase)))) end diff --git a/exercises/reverse-string/reverse-string.jl b/exercises/reverse-string/reverse-string.jl deleted file mode 100644 index 187a3ff7..00000000 --- a/exercises/reverse-string/reverse-string.jl +++ /dev/null @@ -1,3 +0,0 @@ -function myreverse(phrase) - -end diff --git a/exercises/reverse-string/runtests.jl b/exercises/reverse-string/runtests.jl index 616ac3ba..fab0fadb 100644 --- a/exercises/reverse-string/runtests.jl +++ b/exercises/reverse-string/runtests.jl @@ -32,3 +32,14 @@ end @testset "reversing a string twice" begin @test myreverse(myreverse("gift")) == "gift" end + +@testset "emoji" begin + @test myreverse("hi 🐱") == "🐱 ih" +end + +if @isdefined(TEST_GRAPHEMES) + @eval @testset "graphemes" begin + @test myreverse("as⃝df̅") == "f̅ds⃝a" + @test myreverse("hi 👋🏾") == "👋🏾 ih" + end +end