Skip to content

Commit

Permalink
Implement Grains exercise (JuliaLang#89)
Browse files Browse the repository at this point in the history
* 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
HarrisonGrodin authored and SaschaMann committed Oct 8, 2017
1 parent 005a771 commit 02455b4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
13 changes: 11 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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": [
Expand Down
33 changes: 33 additions & 0 deletions exercises/grains/README.md
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.
23 changes: 23 additions & 0 deletions exercises/grains/example.jl
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
9 changes: 9 additions & 0 deletions exercises/grains/grains.jl
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
27 changes: 27 additions & 0 deletions exercises/grains/runtests.jl
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

0 comments on commit 02455b4

Please sign in to comment.