 paulfioravanti's solution

to Grains in the Elm Track

Published at Jul 04 2019 · 0 comments
Instructions
Test suite
Solution

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, with the number of grains doubling on each successive square.

There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).

Write code that shows:

• how many grains were on a given square, and
• the total number of grains on the chessboard

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?

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

\$ elm-test

Automatically run tests again when you save changes:

\$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Source

JavaRanch Cattle Drive, exercise 6 http://www.javaranch.com/grains.jsp

Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

Tests.elm

module Tests exposing (tests)

import Expect
import Grains exposing (square)
import Json.Encode exposing (Value)
import Test exposing (..)

tests : Test
tests =
describe "Grains"
[ describe "square"
[ test "of 1" <|
\() -> Expect.equal (Just 1) (square 1)
, skip <|
test "of 2" <|
\() -> Expect.equal (Just 2) (square 2)
, skip <|
test "of 3" <|
\() -> Expect.equal (Just 4) (square 3)
, skip <|
test "of 4" <|
\() -> Expect.equal (Just 8) (square 4)
, skip <|
test "of 16" <|
\() -> Expect.equal (Just 32768) (square 16)
, skip <|
test "of 32" <|
\() -> Expect.equal (Just 2147483648) (square 32)
, skip <|
test "square 0 raises an exception" <|
\() -> Expect.equal Nothing (square 0)
, skip <|
test "negative square raises an exception" <|
\() -> Expect.equal Nothing (square -1)

{-
Where are the bigger test values?!? Because Javascript's numbers
can't represent values higher than `Number.MAX_SAFE_INTEGER`
(i.e. 9007199254740991), we chose to exclude these final values
to avoid the weirdness. A bit more information can be found
here: https://github.com/elm-lang/elm-compiler/issues/1246
-}
]
]
module Grains exposing (square)

square : Int -> Maybe Int
square n =
if validChessboardSquare n then
Just (calculateSquare n)

else
Nothing

-- PRIVATE

validChessboardSquare : Int -> Bool
validChessboardSquare n =
let
chessboard =
List.range 1 64
in
chessboard
|> List.member n

calculateSquare : Int -> Int
calculateSquare n =
let
base =
2

offset =
1
in
base ^ (n - offset)