🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of michkowalczuk

michkowalczuk's solution

to Grains in the Delphi Pascal Track

Published at Sep 08 2020 · 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?

Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

Loading Exercises into Delphi

If Delphi is properly installed, and *.dpr file types have been associated with Delphi, then double clicking the supplied *.dpr file will start Delphi and load the exercise/project. control + F9 is the keyboard shortcut to compile the project or pressing F9 will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the File drop down menu.

When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the exercism/delphi/<exerciseName> directory.

For example, if you're submitting ubob.pas for the Bob exercise, the submit command would be something like exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas.

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 may request help from a mentor.

uGrainsTests.pas

unit uGrainsTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.1.0';

type
  [TestFixture]
  TgrainsTests = class(TObject)
  public
    [Test]
//  [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure Test_square_1;

    [Test]
    [Ignore]
    procedure Test_square_2;

    [Test]
    [Ignore]
    procedure Test_square_3;

    [Test]
    [Ignore]
    procedure Test_square_4;

    [Test]
    [Ignore]
    procedure Test_square_16;

    [Test]
    [Ignore]
    procedure Test_square_32;

    [Test]
    [Ignore]
    procedure Test_square_64;

    [Test]
    [Ignore]
    procedure Square_0_raises_an_exception;

    [Test]
    [Ignore]
    procedure Negative_square_raises_an_exception;

    [Test]
    [Ignore]
    procedure Square_greater_than_64_raises_an_exception;

    [Test]
    [Ignore]
    procedure Returns_the_total_number_of_grains_on_the_board;
  end;

implementation
uses System.SysUtils, uGrains;

procedure TgrainsTests.Test_square_1;
var expected: UInt64;
begin
  expected := 1;
  Assert.AreEqual(expected, Grains.Square(1));
end;

procedure TgrainsTests.Test_square_2;
var expected: UInt64;
begin
  expected := 2;
  Assert.AreEqual(expected, Grains.Square(2));
end;

procedure TgrainsTests.Test_square_3;
var expected: UInt64;
begin
  expected := 4;
  Assert.AreEqual(expected, Grains.Square(3));
end;

procedure TgrainsTests.Test_square_4;
var expected: UInt64;
begin
  expected := 8;
  Assert.AreEqual(expected, Grains.Square(4));
end;

procedure TgrainsTests.Test_square_16;
var expected: UInt64;
begin
  expected := 32768;
  Assert.AreEqual(expected, Grains.Square(16));
end;

procedure TgrainsTests.Test_square_32;
var expected: UInt64;
begin
  expected := 2147483648;
  Assert.AreEqual(expected, Grains.Square(32));
end;

procedure TgrainsTests.Test_square_64;
var expected: UInt64;
begin
  expected := 9223372036854775808;
  Assert.AreEqual(expected, Grains.Square(64));
end;

procedure TgrainsTests.Square_0_raises_an_exception;
var MyProc: TTestLocalMethod;
begin
  MyProc := procedure
            begin
              Grains.Square(0);
            end;

  Assert.WillRaise(MyProc, ERangeError);
end;

procedure TgrainsTests.Negative_square_raises_an_exception;
var MyProc: TTestLocalMethod;
begin
  MyProc := procedure
            begin
              Grains.Square(-1);
            end;

  Assert.WillRaise(MyProc, ERangeError);
end;

procedure TgrainsTests.Square_greater_than_64_raises_an_exception;
var MyProc: TTestLocalMethod;
begin
  MyProc := procedure
            begin
              Grains.Square(65);
            end;

  Assert.WillRaise(MyProc, ERangeError);
end;

procedure TgrainsTests.Returns_the_total_number_of_grains_on_the_board;
var expected: UInt64;
begin
  expected := 18446744073709551615;
  Assert.AreEqual(expected, Grains.Total);
end;

initialization
  TDUnitX.RegisterTestFixture(TgrainsTests);
end.
unit uGrains;

interface
type

  /// <summary>
  ///   The class that calculates the number of grains of wheat on a chessboard
  ///   given that the number on each square doubles.
  /// </summary>
  Grains = class

    // <summary>
    ///   Calculates how many grains were on a given square.
    /// </summary>
    /// <param name="n">
    ///   given square number
    /// </param>
    /// <returns>
    ///   the number of grains of wheat
    /// </returns>
    class function Square(const n : Integer) : UInt64;

    /// <summary>
    ///   Calculates the total number of grains on the chessboard.
    /// </summary>
    /// <returns>
    ///   the total number of grains
    /// </returns>
    class function Total : UInt64;
  end;

implementation
uses
  System.SysUtils;

class function Grains.Square(const n : Integer) : UInt64;
begin
  if (n > 0) and (n <= 64) then
    Result := Uint64(1) shl (n-1)
  else
    raise ERangeError.Create('ERangeError');
end;

class function Grains.Total : UInt64;
begin
  Result := 2 * Grains.Square(64) - 1;
end;

end.

Community comments

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

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?