From e15429efdae301d623cae0e4ad3ba72739341cae Mon Sep 17 00:00:00 2001 From: Andrej Makarov Date: Wed, 1 Feb 2017 08:24:22 +0000 Subject: [PATCH] Add exercise: secret-handshake --- config.json | 13 ++++- exercises/secret-handshake/example.jl | 12 +++++ exercises/secret-handshake/runtests.jl | 51 +++++++++++++++++++ .../secret-handshake/secret-handshake.jl | 3 ++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 exercises/secret-handshake/example.jl create mode 100644 exercises/secret-handshake/runtests.jl create mode 100644 exercises/secret-handshake/secret-handshake.jl diff --git a/config.json b/config.json index f6952debd4222..ffde4261f8e46 100644 --- a/config.json +++ b/config.json @@ -53,8 +53,8 @@ "difficulty": 1, "topics": [ "control-flow (conditionals)", - "integers", - "mathematics" + "integers", + "mathematics" ] }, { @@ -96,6 +96,15 @@ "integers", "mathematics" ] + }, + { + "slug": "secret-handshake", + "difficulty": 2, + "topics": [ + "arrays", + "filtering", + "mathematics" + ] } ], "deprecated": [ diff --git a/exercises/secret-handshake/example.jl b/exercises/secret-handshake/example.jl new file mode 100644 index 0000000000000..b5ed01e19525b --- /dev/null +++ b/exercises/secret-handshake/example.jl @@ -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 diff --git a/exercises/secret-handshake/runtests.jl b/exercises/secret-handshake/runtests.jl new file mode 100644 index 0000000000000..7a8579ab001f7 --- /dev/null +++ b/exercises/secret-handshake/runtests.jl @@ -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 diff --git a/exercises/secret-handshake/secret-handshake.jl b/exercises/secret-handshake/secret-handshake.jl new file mode 100644 index 0000000000000..556c5c7ac8033 --- /dev/null +++ b/exercises/secret-handshake/secret-handshake.jl @@ -0,0 +1,3 @@ +function secret_handshake(code::Integer) + +end