Skip to content

Commit 78ee9ab

Browse files
authoredMar 8, 2017
Add exercise: rotational-cipher (JuliaLang#44)
* dibs: I will implement exercise rotational-cipher * Add exercise: rotational-cipher * Added rotational-cipher to config * rotational-cipher: test_skip instead of comment * rotational-cipher: revert to comment * Update HINTS-formatting
1 parent 7e06134 commit 78ee9ab

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed
 

Diff for: ‎config.json

+9
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@
215215
"mathematics"
216216
]
217217
},
218+
{
219+
"slug": "rotational-cipher",
220+
"difficulty": 2,
221+
"topics": [
222+
"string literals",
223+
"metaprogramming",
224+
"strings"
225+
]
226+
},
218227
{
219228
"slug": "custom-set",
220229
"difficulty": 5,

Diff for: ‎exercises/rotational-cipher/HINTS.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
This is a good exercise to experiment with non-standard string literals and metaprogramming.
2+
3+
A short introduction to non-standard string literals can be found in this [blog post](http://iaindunning.com/blog/julia-unicode.html). A detailed metaprogramming guide can be found in the [manual](http://docs.julialang.org/en/stable/manual/metaprogramming/).
4+
5+
You can extend your solution by adding the functionality described below. To test your solution, you have to remove the comments at the end of `runtests.jl` before running the tests as usual.
6+
7+
Bonus A only requires basics as outlined in the blog post. Bonus B requires significantly more knowledge of metaprogramming in Julia.
8+
9+
## Bonus A
10+
Implement a string literal that acts as `ROT13` on the string:
11+
```julia
12+
R13"abcdefghijklmnopqrstuvwxyz" == "nopqrstuvwxyzabcdefghijklm"
13+
```
14+
15+
## Bonus B
16+
Implement string literals `R<i>`, `i = 0, ..., 26`, that shift the string for `i` values:
17+
```julia
18+
R0"Hello, World!" == "Hello, World!"
19+
R4"Testing 1 2 3 testing" == "Xiwxmrk 1 2 3 xiwxmrk"
20+
R13"abcdefghijklmnopqrstuvwxyz" == "nopqrstuvwxyzabcdefghijklm"
21+
```

Diff for: ‎exercises/rotational-cipher/example.jl

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function rotate(n::Int, c::Char)
2+
if c in 'a':'z'
3+
c = 'a' + (c - 'a' + n) % 26
4+
elseif c in 'A':'Z'
5+
c = 'A' + (c - 'A' + n) % 26
6+
end
7+
return c
8+
end
9+
10+
rotate(n::Int, s::String) = join(rotate(n, c) for c in s)
11+
12+
for n in 0:26
13+
eval( :(macro $(Symbol(:R, n, :_str))(s::String)
14+
:(rotate($$n, $s))
15+
end))
16+
end

Diff for: ‎exercises/rotational-cipher/rotational-cipher.jl

Whitespace-only changes.

Diff for: ‎exercises/rotational-cipher/runtests.jl

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Base.Test
2+
3+
include("rotational-cipher.jl")
4+
5+
@testset "rotate function" begin
6+
@testset "rotate by n" begin
7+
@testset "no wrap" begin
8+
@test rotate(1, "a") == "b"
9+
@test rotate(1, 'a') == 'b'
10+
@test rotate(13, "m") == "z"
11+
@test rotate(13, 'm') == 'z'
12+
end
13+
@testset "wrap around" begin
14+
@test rotate(13, "n") == "a"
15+
@test rotate(13, 'n') == 'a'
16+
end
17+
end
18+
19+
@testset "full rotation" begin
20+
@test rotate(26, "a") == "a"
21+
@test rotate(26, 'a') == 'a'
22+
@test rotate(0, "a") == "a"
23+
@test rotate(0, 'a') == 'a'
24+
end
25+
26+
@testset "full strings" begin
27+
@test rotate(5, "OMG") == "TRL"
28+
@test rotate(5, "O M G") == "T R L"
29+
@test rotate(4, "Testing 1 2 3 testing") == "Xiwxmrk 1 2 3 xiwxmrk"
30+
@test rotate(21, "Let's eat, Grandma!") == "Gzo'n zvo, Bmviyhv!"
31+
@test rotate(13, "The quick brown fox jumps over the lazy dog.") == "Gur dhvpx oebja sbk whzcf bire gur ynml qbt."
32+
end
33+
end
34+
35+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
36+
# Additional exercises #
37+
# Remove the comments for the optional bonus exercises from HINTS.md #
38+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
39+
40+
# Bonus A
41+
# @testset "string literal R13" begin
42+
# @test R13"The quick brown fox jumps over the lazy dog." == "Gur dhvpx oebja sbk whzcf bire gur ynml qbt."
43+
# end
44+
45+
# Bonus B
46+
# @testset "string literals" begin
47+
# @test R5"OMG" == "TRL"
48+
# @test R4"Testing 1 2 3 testing" == "Xiwxmrk 1 2 3 xiwxmrk"
49+
# @test R21"Let's eat, Grandma!" == "Gzo'n zvo, Bmviyhv!"
50+
# @test R13"The quick brown fox jumps over the lazy dog." == "Gur dhvpx oebja sbk whzcf bire gur ynml qbt."
51+
# end

0 commit comments

Comments
 (0)