Skip to content

Commit

Permalink
reverse-string: encourage grapheme awareness (#261)
Browse files Browse the repository at this point in the history
Closes #238
  • Loading branch information
cmcaine authored Sep 29, 2020
1 parent 87e24ce commit 6313e70
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
13 changes: 13 additions & 0 deletions exercises/reverse-string/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions exercises/reverse-string/example.jl
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions exercises/reverse-string/reverse-string.jl

This file was deleted.

11 changes: 11 additions & 0 deletions exercises/reverse-string/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6313e70

Please sign in to comment.