From 6a24e9120ab7581dcc6c1bf993e91f1e607ab5da Mon Sep 17 00:00:00 2001 From: Andrej Makarov Date: Tue, 31 Jan 2017 14:19:53 +0000 Subject: [PATCH] Add exercise: trinary --- config.json | 12 ++++++++- exercises/trinary/example.jl | 4 +++ exercises/trinary/runtests.jl | 47 +++++++++++++++++++++++++++++++++++ exercises/trinary/trinary.jl | 3 +++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 exercises/trinary/example.jl create mode 100644 exercises/trinary/runtests.jl create mode 100644 exercises/trinary/trinary.jl diff --git a/config.json b/config.json index 656f5ef2f96dd..77abde8a5a87a 100644 --- a/config.json +++ b/config.json @@ -53,7 +53,7 @@ "difficulty": 1, "topics": [ "control-flow (conditionals)", - "integers", + "integers", "mathematics" ] }, @@ -76,6 +76,16 @@ "unicode", "control-flow (if-else statements)" ] + }, + { + "slug": "trinary", + "difficulty": 2, + "topics": [ + "arrays", + "strings", + "integers", + "mathematics" + ] } ], "deprecated": [ diff --git a/exercises/trinary/example.jl b/exercises/trinary/example.jl new file mode 100644 index 0000000000000..8b37a77aeb520 --- /dev/null +++ b/exercises/trinary/example.jl @@ -0,0 +1,4 @@ +function trinary_to_decimal(str::AbstractString) + typeof(match(r"^[0-2]+$", str)) == Void && return 0 + mapreduce(i->(i[2]-'0')*3^i[1], +, zip(0:length(str), reverse(str))) +end diff --git a/exercises/trinary/runtests.jl b/exercises/trinary/runtests.jl new file mode 100644 index 0000000000000..c676b1f2a150c --- /dev/null +++ b/exercises/trinary/runtests.jl @@ -0,0 +1,47 @@ +using Base.Test + +include("trinary.jl") + +@testset "trinary 1 is decimal 1" begin + @test trinary_to_decimal("1") == 1 +end + +@testset "trinary 2 is decimal 2" begin + @test trinary_to_decimal("2") == 2 +end + +@testset "trinary 10 is decimal 3" begin + @test trinary_to_decimal("10") == 3 +end + +@testset "trinary 11 is decimal 4" begin + @test trinary_to_decimal("11") == 4 +end + +@testset "trinary 100 is decimal 9" begin + @test trinary_to_decimal("100") == 9 +end + +@testset "trinary 112 is decimal 14" begin + @test trinary_to_decimal("112") == 14 +end + +@testset "trinary 222 is decimal 26" begin + @test trinary_to_decimal("222") == 26 +end + +@testset "trinary 1122000120 is decimal 32091" begin + @test trinary_to_decimal("1122000120") == 32091 +end + +@testset "invalid trinary digits returns 0" begin + @test trinary_to_decimal("1234") == 0 +end + +@testset "invalid word as input returns 0" begin + @test trinary_to_decimal("carrot") == 0 +end + +@testset "invalid numbers with letters as input returns 0" begin + @test trinary_to_decimal("0a1b2c") == 0 +end diff --git a/exercises/trinary/trinary.jl b/exercises/trinary/trinary.jl new file mode 100644 index 0000000000000..1f3562807a8cb --- /dev/null +++ b/exercises/trinary/trinary.jl @@ -0,0 +1,3 @@ +function trinary_to_decimal(str::AbstractString) + +end