From 9c53315e46cc5ee948d316f80b49847fcd4209e6 Mon Sep 17 00:00:00 2001 From: Andrej Makarov Date: Tue, 31 Jan 2017 06:57:00 +0000 Subject: [PATCH] Add exercise: scrabble-score --- config.json | 12 ++++- exercises/scrabble-score/example.jl | 9 ++++ exercises/scrabble-score/runtests.jl | 51 ++++++++++++++++++++++ exercises/scrabble-score/scrabble-score.jl | 3 ++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 exercises/scrabble-score/example.jl create mode 100644 exercises/scrabble-score/runtests.jl create mode 100644 exercises/scrabble-score/scrabble-score.jl diff --git a/config.json b/config.json index 656f5ef2f96dd..a05ab3f3006fd 100644 --- a/config.json +++ b/config.json @@ -53,10 +53,20 @@ "difficulty": 1, "topics": [ "control-flow (conditionals)", - "integers", + "integers", "mathematics" ] }, + { + "slug": "scrabble-score", + "difficulty": 1, + "topics": [ + "control-flow (loops)", + "control-flow (conditionals)", + "arrays", + "strings" + ] + }, { "slug": "anagram", "difficulty": 2, diff --git a/exercises/scrabble-score/example.jl b/exercises/scrabble-score/example.jl new file mode 100644 index 0000000000000..f0def35b5022b --- /dev/null +++ b/exercises/scrabble-score/example.jl @@ -0,0 +1,9 @@ +function score(str::AbstractString) + rank = Dict('a'=>1, 'e'=>1, 'i'=>1, 'o'=>1, 'u'=>1, 'l'=>1, + 'n'=>1, 'r'=>1, 's'=>1, 't'=>1, 'd'=>2, 'g'=>2, + 'b'=>3, 'c'=>3, 'm'=>3, 'p'=>3, 'f'=>4, 'h'=>4, + 'v'=>4, 'w'=>4, 'y'=>4, 'k'=>5, 'j'=>8, 'x'=>8, + 'q'=>10, 'z'=>10) + length(str) == 0 && return 0 + mapreduce(x->get(rank, x, 0), +, lowercase(str)) +end diff --git a/exercises/scrabble-score/runtests.jl b/exercises/scrabble-score/runtests.jl new file mode 100644 index 0000000000000..fca924cec1d77 --- /dev/null +++ b/exercises/scrabble-score/runtests.jl @@ -0,0 +1,51 @@ +using Base.Test + +include("scrabble-score.jl") + +@testset "lowercase letter" begin + @test score("a") == 1 +end + +@testset "uppercase letter" begin + @test score("A") == 1 +end + +@testset "valuable letter" begin + @test score("f") == 4 +end + +@testset "short word" begin + @test score("at") == 2 +end + +@testset "short, valuable word" begin + @test score("zoo") == 12 +end + +@testset "medium word" begin + @test score("street") == 6 +end + +@testset "medium, valuable word" begin + @test score("quirky") == 22 +end + +@testset "long, mixed-case word" begin + @test score("OxyphenButazone") == 41 +end + +@testset "english-like word" begin + @test score("pinata") == 8 +end + +@testset "non-english letter is not scored" begin + @test score("piƱata") == 7 +end + +@testset "empty input" begin + @test score("") == 0 +end + +@testset "entire alphabet available" begin + @test score("abcdefghijklmnopqrstuvwxyz") == 87 +end diff --git a/exercises/scrabble-score/scrabble-score.jl b/exercises/scrabble-score/scrabble-score.jl new file mode 100644 index 0000000000000..c01296a0a083a --- /dev/null +++ b/exercises/scrabble-score/scrabble-score.jl @@ -0,0 +1,3 @@ +function score(str::AbstractString) + +end