Skip to content

Commit

Permalink
Merge pull request JuliaLang#25 from andrej-makarov-skrt/ex-secret-ha…
Browse files Browse the repository at this point in the history
…ndshake

Add exercise: secret-handshake
  • Loading branch information
SaschaMann committed Feb 1, 2017
2 parents 4c48dc6 + e15429e commit 4420135
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
13 changes: 11 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
"difficulty": 1,
"topics": [
"control-flow (conditionals)",
"integers",
"mathematics"
"integers",
"mathematics"
]
},
{
Expand Down Expand Up @@ -105,6 +105,15 @@
"integers",
"mathematics"
]
},
{
"slug": "secret-handshake",
"difficulty": 2,
"topics": [
"arrays",
"filtering",
"mathematics"
]
}
],
"deprecated": [
Expand Down
12 changes: 12 additions & 0 deletions exercises/secret-handshake/example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function secret_handshake(code::Integer)
# NOTE: Handle wrong codes
# code < 0 && error("Invalid secret handshake code")
# NOTE: Handle common actions
masks = [(1, "wink"), (2, "double blink"),
(4, "close your eyes"), (8, "jump")]
actions = map(x->code&x[1]==x[1]&&x[2], masks)
filter!(x->x!=false, actions)
# NOTE: Handle reversing bitmask
code&16==16 && reverse!(actions)
actions
end
51 changes: 51 additions & 0 deletions exercises/secret-handshake/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Base.Test

include("secret-handshake.jl")

@testset "wink for 1" begin
@test secret_handshake(1) == ["wink"]
end

@testset "double blink for 10" begin
@test secret_handshake(2) == ["double blink"]
end

@testset "close your eyes for 100" begin
@test secret_handshake(4) == ["close your eyes"]
end

@testset "jump for 1000" begin
@test secret_handshake(8) == ["jump"]
end

@testset "combine two actions" begin
@test secret_handshake(3) == ["wink", "double blink"]
end

@testset "reverse two actions" begin
@test secret_handshake(19) == ["double blink", "wink"]
end

@testset "reversing one action gives the same action" begin
@test secret_handshake(24) == ["jump"]
end

@testset "reversing no actions still gives no actions" begin
@test secret_handshake(16) == []
end

@testset "all possible actions" begin
@test secret_handshake(15) == ["wink", "double blink", "close your eyes", "jump"]
end

@testset "reverse all possible actions" begin
@test secret_handshake(31) == ["jump", "close your eyes", "double blink", "wink"]
end

@testset "do nothing for zero" begin
@test secret_handshake(0) == []
end

@testset "do nothing if lower 5 bits not set" begin
@test secret_handshake(32) == []
end
3 changes: 3 additions & 0 deletions exercises/secret-handshake/secret-handshake.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function secret_handshake(code::Integer)

end

0 comments on commit 4420135

Please sign in to comment.