Skip to content

armstrong-numbers: New exercise #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@
"unlocked_by": null,
"uuid": "edfc2903-1165-4029-9901-0be72b32865f"
},
{
"core": false,
"difficulty": 2,
"slug": "armstrong-numbers",
"topics": [
"algorithms"
],
"unlocked_by": "collatz-conjecture",
"uuid": "5315f8b0-f2a2-4750-8dc3-b4d182111036"
},
{
"core": false,
"difficulty": 3,
Expand Down
60 changes: 60 additions & 0 deletions exercises/armstrong-numbers/ArmstrongNumbers.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
program ArmstrongNumbers;

{$IFNDEF TESTINSIGHT}
{$APPTYPE CONSOLE}
{$ENDIF}{$STRONGLINKTYPES ON}
uses
System.SysUtils,
{$IFDEF TESTINSIGHT}
TestInsight.DUnitX,
{$ENDIF }
DUnitX.Loggers.Console,
DUnitX.Loggers.Xml.NUnit,
DUnitX.TestFramework,
uArmstrongNumbersTests in 'uArmstrongNumbersTests.pas',
uArmstrongNumbers in 'uArmstrongNumbers.pas';

var
runner : ITestRunner;
results : IRunResults;
logger : ITestLogger;
nunitLogger : ITestLogger;
begin
{$IFDEF TESTINSIGHT}
TestInsight.DUnitX.RunRegisteredTests;
exit;
{$ENDIF}
try
//Check command line options, will exit if invalid
TDUnitX.CheckCommandLine;
//Create the test runner
runner := TDUnitX.CreateRunner;
//Tell the runner to use RTTI to find Fixtures
runner.UseRTTI := True;
//tell the runner how we will log things
//Log to the console window
logger := TDUnitXConsoleLogger.Create(true);
runner.AddLogger(logger);
//Generate an NUnit compatible XML File
nunitLogger := TDUnitXXMLNUnitFileLogger.Create(TDUnitX.Options.XMLOutputFile);
runner.AddLogger(nunitLogger);
runner.FailsOnNoAsserts := False; //When true, Assertions must be made during tests;

//Run tests
results := runner.Execute;
if not results.AllPassed then
System.ExitCode := EXIT_ERRORS;

{$IFNDEF CI}
//We don't want this happening when running under CI.
if TDUnitX.Options.ExitBehavior = TDUnitXExitBehavior.Pause then
begin
System.Write('Done.. press <Enter> key to quit.');
System.Readln;
end;
{$ENDIF}
except
on E: Exception do
System.Writeln(E.ClassName, ': ', E.Message);
end;
end.
39 changes: 39 additions & 0 deletions exercises/armstrong-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Armstrong Numbers

An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that is the sum of its own digits each raised to the power of the number of digits.

For example:

- 9 is an Armstrong number, because `9 = 9^1 = 9`
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 2`
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`

Write some code to determine whether a number is an Armstrong number.

## Testing

In order to run the tests for this track, you will need to install
DUnitX. Please see the [installation](http://www.exercism.io/languages/delphi/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](https://gitter.im/exercism/Pascal-Delphi) support room on [gitter.im](https://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

Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
24 changes: 24 additions & 0 deletions exercises/armstrong-numbers/uArmstrongNumbersExample.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
unit uArmstrongNumbers;

interface
uses System.SysUtils, System.Math;

function isArmstrongNumber(aNumber: integer): Boolean;

implementation

function isArmstrongNumber(aNumber: integer): Boolean;
var wrkString: string;
Exponent: integer;
wrkInteger: integer;
i: integer;
begin
wrkString := aNumber.ToString;
Exponent := length(wrkString);
wrkInteger := 0;
for i := low(wrkString) to high(wrkString) do
wrkInteger := wrkInteger + trunc(Power(string.ToInteger(wrkString[i]), Exponent));
result := wrkInteger = aNumber;
end;

end.
101 changes: 101 additions & 0 deletions exercises/armstrong-numbers/uArmstrongNumbersTests.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
unit uArmstrongNumbersTests;

interface
uses
DUnitX.TestFramework;

const
CanonicalVersion = '1.0.0';

type

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

[Test]
[Ignore]
procedure There_are_no_2_digit_Armstrong_numbers;

[Test]
[Ignore]
procedure Three_digit_number_that_is_an_Armstrong_number;

[Test]
[Ignore]
procedure Three_digit_number_that_is_not_an_Armstrong_number;

[Test]
[Ignore]
procedure Four_digit_number_that_is_an_Armstrong_number;

[Test]
[Ignore]
procedure Four_digit_number_that_is_not_an_Armstrong_number;

[Test]
[Ignore]
procedure Seven_digit_number_that_is_an_Armstrong_number;

[Test]
[Ignore]
procedure Seven_digit_number_that_is_not_an_Armstrong_number;
end;

implementation
uses uArmstrongNumbers;

{ ArmstrongNumbersTests }

procedure ArmstrongNumbersTests.Four_digit_number_that_is_an_Armstrong_number;
begin
Assert.IsTrue(isArmstrongNumber(9474));
end;

procedure ArmstrongNumbersTests.Four_digit_number_that_is_not_an_Armstrong_number;
begin
Assert.IsFalse(isArmstrongNumber(9475));
end;

procedure ArmstrongNumbersTests.Seven_digit_number_that_is_an_Armstrong_number;
begin
Assert.IsTrue(isArmstrongNumber(9926315));
end;

procedure ArmstrongNumbersTests.Seven_digit_number_that_is_not_an_Armstrong_number;
begin
Assert.IsFalse(isArmstrongNumber(9926314));
end;

procedure ArmstrongNumbersTests.Single_digit_numbers_are_Armstrong_numbers;
var
i: integer;
begin
for i := 0 to 9 do
Assert.IsTrue(isArmstrongNumber(i));
end;

procedure ArmstrongNumbersTests.There_are_no_2_digit_Armstrong_numbers;
var
i: integer;
begin
for i := 10 to 99 do
Assert.IsFalse(isArmstrongNumber(i));
end;

procedure ArmstrongNumbersTests.Three_digit_number_that_is_an_Armstrong_number;
begin
Assert.IsTrue(isArmstrongNumber(153));
end;

procedure ArmstrongNumbersTests.Three_digit_number_that_is_not_an_Armstrong_number;
begin
Assert.IsFalse(isArmstrongNumber(100));
end;

initialization
TDUnitX.RegisterTestFixture(ArmstrongNumbersTests);
end.