Skip to content

Commit

Permalink
Merge pull request JuliaLang#22 from andrej-makarov-skrt/ex-scrabble-…
Browse files Browse the repository at this point in the history
…score

Add exercise: scrabble-score
  • Loading branch information
SaschaMann committed Jan 31, 2017
2 parents 53931eb + 9c53315 commit 119aa35
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
12 changes: 11 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions exercises/scrabble-score/example.jl
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions exercises/scrabble-score/runtests.jl
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions exercises/scrabble-score/scrabble-score.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function score(str::AbstractString)

end

0 comments on commit 119aa35

Please sign in to comment.