Avatar of lujanfernaud

lujanfernaud's solution

to Grains in the Ruby Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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, and
  • the total number of grains

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?


For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby grains_test.rb

To include color from the command line:

ruby -r minitest/pride grains_test.rb

Source

JavaRanch Cattle Drive, exercise 6 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.

grains_test.rb

require 'minitest/autorun'
require_relative 'grains'

# Common test data version: 1.0.0 2e0e77e
class GrainsTest < Minitest::Test
  def test_1
    # skip
    assert_equal 1, Grains.square(1)
  end

  def test_2
    skip
    assert_equal 2, Grains.square(2)
  end

  def test_3
    skip
    assert_equal 4, Grains.square(3)
  end

  def test_4
    skip
    assert_equal 8, Grains.square(4)
  end

  def test_16
    skip
    assert_equal 32_768, Grains.square(16)
  end

  def test_32
    skip
    assert_equal 2_147_483_648, Grains.square(32)
  end

  def test_64
    skip
    assert_equal 9_223_372_036_854_775_808, Grains.square(64)
  end

  def test_square_0_raises_an_exception
    skip
    assert_raises(ArgumentError) { Grains.square(0) }
  end

  def test_negative_square_raises_an_exception
    skip
    assert_raises(ArgumentError) { Grains.square(-1) }
  end

  def test_square_greater_than_64_raises_an_exception
    skip
    assert_raises(ArgumentError) { Grains.square(65) }
  end

  def test_returns_the_total_number_of_grains_on_the_board
    skip
    assert_equal 18_446_744_073_709_551_615, Grains.total
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 1, BookKeeping::VERSION
  end
end
class Grains
  class << self
    CHESSBOARD = (1..64)
    CHESSBOARD_SQUARES = 64
    GRAINS_MULTIPLIER = 2
    OFFSET = 1

    def on_square(square_number)
      not_a_chessboard_square unless CHESSBOARD.cover?(square_number)

      GRAINS_MULTIPLIER**(square_number - OFFSET)
    end

    def total
      GRAINS_MULTIPLIER**CHESSBOARD_SQUARES - OFFSET
    end

    def not_a_chessboard_square
      raise ArgumentError, "Square number must be between 1 and 64"
    end

    alias square on_square
  end
end

module BookKeeping
  VERSION = 1
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of lujanfernaud

On this iteration I tried to avoid magic numbers, made the ArgumentError message more specific, and used an alias to make the square method name more clear. Grains.on_square(2) => 4

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?