diff --git a/config.json b/config.json index 9805f68b..dc770b01 100644 --- a/config.json +++ b/config.json @@ -2,8 +2,7 @@ "language": "Julia", "active": true, "blurb": "Julia is an open-source high-level, dynamic programming language whose sweet spot is technical and scientific computing. It is convenient for day-to-day work and fast enough for high performance computing.", - "exercises": [ - { + "exercises": [{ "uuid": "a668410d-41aa-4710-a68f-54521da6486d", "slug": "hello-world", "core": true, @@ -358,6 +357,16 @@ "mathematics", "iterators" ] + }, + { + "uuid": "2ed6e171-04e1-b080-f383-a801b2564b3e0f4f1ee", + "slug": "grains", + "core": false, + "unlocked_by": "difference-of-squares", + "difficulty": 1, + "topics": [ + "exceptions" + ] } ], "foregone": [ diff --git a/exercises/grains/README.md b/exercises/grains/README.md new file mode 100644 index 00000000..9e1ba94f --- /dev/null +++ b/exercises/grains/README.md @@ -0,0 +1,33 @@ +# Grains + +Calculate the number of grains of wheat on a chessboard given that the number +on each square doubles. + +There once was a wise servant who saved the life of a prince. The king +promised to pay whatever the servant could dream up. Knowing that the +king loved chess, the servant told the king he would like to have grains +of wheat. One grain on the first square of a chess board. Two grains on +the next. Four on the third, and so on. + +There are 64 squares on a chessboard. + +Write code that shows: +- how many grains were on each square (`on_square`) +- the total number of grains (`total_after`) + +## For bonus points + +Did you get the tests passing and the code clean? If you want to, these +are some additional things you could try: + +- Optimize for speed. +- Optimize for readability. + +Then please share your thoughts in a comment on the submission. Did this +experiment make the code better? Worse? Did you learn anything from it? + +## Source +JavaRanch Cattle Drive, exercise 6 [http://www.javaranch.com/grains.jsp](http://www.javaranch.com/grains.jsp) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/grains/example.jl b/exercises/grains/example.jl new file mode 100644 index 00000000..ef7f9add --- /dev/null +++ b/exercises/grains/example.jl @@ -0,0 +1,23 @@ +"""Calculate the number of grains on square `square`.""" +function on_square(square) + check_square_input(square) + 2 ^ (square - 1) +end + +"""Calculate the total number of grains after square `square`.""" +function total_after(square) + check_square_input(square) + sum(map(on_square, 1:square)) +end + +"""Validate an arbitrary square.""" +function check_square_input(square) + square == 0 && throw(DomainError(square, "Square input of zero is invalid.")) + square < 0 && throw(DomainError(square, "Negative square input is invalid.")) + square > 64 && throw(DomainError(square, "Square input greater than 64 is invalid.")) +end + +if VERSION < v"0.7" # backwards compatibility + Base.DomainError(val) = DomainError() + Base.DomainError(val, msg) = DomainError() +end diff --git a/exercises/grains/grains.jl b/exercises/grains/grains.jl new file mode 100644 index 00000000..c0746c5a --- /dev/null +++ b/exercises/grains/grains.jl @@ -0,0 +1,9 @@ +"""Calculate the number of grains on square `square`.""" +function on_square(square) + +end + +"""Calculate the total number of grains after square `square`.""" +function total_after(square) + +end diff --git a/exercises/grains/runtests.jl b/exercises/grains/runtests.jl new file mode 100644 index 00000000..cdc259d1 --- /dev/null +++ b/exercises/grains/runtests.jl @@ -0,0 +1,27 @@ +using Base.Test + +include("grains.jl") + +@testset "On squares" begin + @testset "On square $s" for s = UInt64(1):64 + @test on_square(s) == 2^(s-1) + @test total_after(s) == 2^s - 1 + end +end + +@testset "Invalid values" begin + @testset "Zero" begin + @test_throws DomainError on_square(0) + @test_throws DomainError total_after(0) + end + + @testset "Negative" begin + @test_throws DomainError on_square(-1) + @test_throws DomainError total_after(-1) + end + + @testset "Greater than 64" begin + @test_throws DomainError on_square(65) + @test_throws DomainError total_after(65) + end +end