forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request JuliaLang#25 from andrej-makarov-skrt/ex-secret-ha…
…ndshake Add exercise: secret-handshake
- Loading branch information
Showing
4 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function secret_handshake(code::Integer) | ||
|
||
end |