v2000vova's solution

to Grains in the Delphi Pascal Track

Published at Apr 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.

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.

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
Grains = class
class function Square(aNumber: Integer) : UInt64;
class function Total() : UInt64;
end;

implementation

uses
SysUtils,
System.SysConst,
Math;

class function Grains.Square(aNumber: Integer): UInt64;
begin
if (aNumber < 1) Or (aNumber > 64) then
raise ERangeError.CreateRes(@SRangeError);

if aNumber = 1 then
Result := 1
else begin
var E : Extended := IntPower(2.0, aNumber - 2);
Result := UInt64(Round(E)) * 2;
end;
end;

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

end.``````