From 4c48dc6d92c045520ba25548b991e6ed0826d175 Mon Sep 17 00:00:00 2001 From: Andrej Makarov Date: Wed, 1 Feb 2017 23:47:37 +0000 Subject: [PATCH] Add exercise: raindrops (#20) --- config.json | 9 ++++ exercises/raindrops/example.jl | 16 +++++++ exercises/raindrops/raindrops.jl | 3 ++ exercises/raindrops/runtests.jl | 72 ++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 exercises/raindrops/example.jl create mode 100644 exercises/raindrops/raindrops.jl create mode 100644 exercises/raindrops/runtests.jl diff --git a/config.json b/config.json index f6952debd4222..36d5fbfbb0ff1 100644 --- a/config.json +++ b/config.json @@ -57,6 +57,15 @@ "mathematics" ] }, + { + "slug": "raindrops", + "difficulty": 1, + "topics": [ + "control-flow (loops)", + "arrays", + "strings" + ] + }, { "slug": "scrabble-score", "difficulty": 1, diff --git a/exercises/raindrops/example.jl b/exercises/raindrops/example.jl new file mode 100644 index 0000000000000..128bbd5bad9a5 --- /dev/null +++ b/exercises/raindrops/example.jl @@ -0,0 +1,16 @@ +# If the number has 3 as a factor, output 'Pling'. +# If the number has 5 as a factor, output 'Plang'. +# If the number has 7 as a factor, output 'Plong'. +# If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through. + +function raindrops(number::Int) + drops = [] + number % 3 == 0 && push!(drops, "Pling") + number % 5 == 0 && push!(drops, "Plang") + number % 7 == 0 && push!(drops, "Plong") + if size(drops, 1) == 0 + repr(number) + else + join(drops) + end +end diff --git a/exercises/raindrops/raindrops.jl b/exercises/raindrops/raindrops.jl new file mode 100644 index 0000000000000..2f98304b845d3 --- /dev/null +++ b/exercises/raindrops/raindrops.jl @@ -0,0 +1,3 @@ +function raindrops(number::Int) + +end diff --git a/exercises/raindrops/runtests.jl b/exercises/raindrops/runtests.jl new file mode 100644 index 0000000000000..82700923ded56 --- /dev/null +++ b/exercises/raindrops/runtests.jl @@ -0,0 +1,72 @@ +using Base.Test + +include("raindrops.jl") + +@testset "detect numbers" begin + @testset "the sound for 1 is 1" begin + @test raindrops(1) == "1" + end + @testset "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base" begin + @test raindrops(8) == "8" + end + @testset "the sound for 52 is 52" begin + @test raindrops(52) == "52" + end +end + +@testset "detect pling" begin + @testset "the sound for 3 is Pling" begin + @test raindrops(3) == "Pling" + end + @testset "the sound for 6 is Pling as it has a factor 3" begin + @test raindrops(6) == "Pling" + end + @testset "the sound for 9 is Pling as it has a factor 3" begin + @test raindrops(9) == "Pling" + end + @testset "the sound for 27 is Pling as it has a factor 3" begin + @test raindrops(27) == "Pling" + end +end + +@testset "detect plang" begin + @testset "the sound for 5 is Plang" begin + @test raindrops(5) == "Plang" + end + @testset "the sound for 10 is Plang as it has a factor 5" begin + @test raindrops(10) == "Plang" + end + @testset "the sound for 25 is Plang as it has a factor 5" begin + @test raindrops(25) == "Plang" + end + @testset "the sound for 3125 is Plang as it has a factor 5" begin + @test raindrops(3125) == "Plang" + end +end + +@testset "detect plong" begin + @testset "the sound for 7 is Plong" begin + @test raindrops(7) == "Plong" + end + @testset "the sound for 14 is Plong as it has a factor of 7" begin + @test raindrops(14) == "Plong" + end + @testset "the sound for 49 is Plong as it has a factor 7" begin + @test raindrops(49) == "Plong" + end +end + +@testset "detect multiple sounds" begin + @testset "the sound for 15 is PlingPlang as it has factors 3 and 5" begin + @test raindrops(15) == "PlingPlang" + end + @testset "the sound for 21 is PlingPlong as it has factors 3 and 7" begin + @test raindrops(21) == "PlingPlong" + end + @testset "the sound for 35 is PlangPlong as it has factors 5 and 7" begin + @test raindrops(35) == "PlangPlong" + end + @testset "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7" begin + @test raindrops(105) == "PlingPlangPlong" + end +end