forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Grains exercise (JuliaLang#89)
* Implement Grains exercise Contributes to exercism/julia#87. Loosely based on Python implementation. * Revert to pre-0.7 feature set In v0.6-, `DomainError` takes no arguments. * Add exercise to config.json Update README according to specification. * Add exercise dependency and fix README * Fix bug and add backwards compatibility
- Loading branch information
1 parent
005a771
commit 02455b4
Showing
5 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |