Skip to content

Commit

Permalink
Sync armstrong-numbers (exercism#831)
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
tomasnorre authored Oct 30, 2024
1 parent 23f2cd7 commit abf3d49
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 59 deletions.
8 changes: 5 additions & 3 deletions exercises/practice/armstrong-numbers/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Instructions

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.
An [Armstrong number][armstrong-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 = 1`
- 10 is _not_ an Armstrong number, because `10 != 1^2 + 0^2 = 1`
- 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`
- 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.

[armstrong-number]: https://en.wikipedia.org/wiki/Narcissistic_number
2 changes: 1 addition & 1 deletion exercises/practice/armstrong-numbers/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
".meta/example.php"
]
},
"blurb": "Determine if a number is an Armstrong number",
"blurb": "Determine if a number is an Armstrong number.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Narcissistic_number"
}
22 changes: 0 additions & 22 deletions exercises/practice/armstrong-numbers/.meta/example.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

function isArmstrongNumber(int $number): bool
Expand Down
39 changes: 28 additions & 11 deletions exercises/practice/armstrong-numbers/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[c1ed103c-258d-45b2-be73-d8c6d9580c7b]
description = "Zero is an Armstrong number"

[579e8f03-9659-4b85-a1a2-d64350f6b17a]
description = "Single digit numbers are Armstrong numbers"
description = "Single-digit numbers are Armstrong numbers"

[2d6db9dc-5bf8-4976-a90b-b2c2b9feba60]
description = "There are no 2 digit Armstrong numbers"
description = "There are no two-digit Armstrong numbers"

[509c087f-e327-4113-a7d2-26a4e9d18283]
description = "Three digit number that is an Armstrong number"
description = "Three-digit number that is an Armstrong number"

[7154547d-c2ce-468d-b214-4cb953b870cf]
description = "Three digit number that is not an Armstrong number"
description = "Three-digit number that is not an Armstrong number"

[6bac5b7b-42e9-4ecb-a8b0-4832229aa103]
description = "Four digit number that is an Armstrong number"
description = "Four-digit number that is an Armstrong number"

[eed4b331-af80-45b5-a80b-19c9ea444b2e]
description = "Four digit number that is not an Armstrong number"
description = "Four-digit number that is not an Armstrong number"

[f971ced7-8d68-4758-aea1-d4194900b864]
description = "Seven digit number that is an Armstrong number"
description = "Seven-digit number that is an Armstrong number"

[7ee45d52-5d35-4fbd-b6f1-5c8cd8a67f18]
description = "Seven digit number that is not an Armstrong number"
description = "Seven-digit number that is not an Armstrong number"

[5ee2fdf8-334e-4a46-bb8d-e5c19c02c148]
description = "Armstrong number containing seven zeroes"
include = false
comment = "PHP does not support integers > 64bit in its standard library"

[12ffbf10-307a-434e-b4ad-c925680e1dd4]
description = "The largest and last Armstrong number"
include = false
comment = "PHP does not support integers > 64bit in its standard library"
58 changes: 36 additions & 22 deletions exercises/practice/armstrong-numbers/ArmstrongNumbersTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

class ArmstrongNumbersTest extends PHPUnit\Framework\TestCase
Expand All @@ -31,46 +9,82 @@ public static function setUpBeforeClass(): void
require_once 'ArmstrongNumbers.php';
}

/**
* uuid: c1ed103c-258d-45b2-be73-d8c6d9580c7b
* @testdox Zero is an Armstrong number
*/
public function testZero(): void
{
$this->assertTrue(isArmstrongNumber(0));
}

/**
* uuid: 579e8f03-9659-4b85-a1a2-d64350f6b17a
* @testdox Single-digit numbers are Armstrong numbers
*/
public function testSingleDigit(): void
{
$this->assertTrue(isArmstrongNumber(5));
}

/**
* uuid: 2d6db9dc-5bf8-4976-a90b-b2c2b9feba60
* @testdox There are no two-digit Armstrong numbers
*/
public function testDoubleDigit(): void
{
$this->assertFalse(isArmstrongNumber(10));
}

/**
* uuid: 509c087f-e327-4113-a7d2-26a4e9d18283
* @testdox Three-digit number that is an Armstrong number
*/
public function testThreeDigitIsArmstrongNumber(): void
{
$this->assertTrue(isArmstrongNumber(153));
}

/**
* uuid: 7154547d-c2ce-468d-b214-4cb953b870cf
* @testdox Three-digit number that is not an Armstrong number
*/
public function testThreeDigitIsNotArmstrongNumber(): void
{
$this->assertFalse(isArmstrongNumber(100));
}

/**
* uuid: 6bac5b7b-42e9-4ecb-a8b0-4832229aa103
* @testdox Four-digit number that is an Armstrong number
*/
public function testFourDigitIsArmstrongNumber(): void
{
$this->assertTrue(isArmstrongNumber(9474));
}

/**
* uuid: eed4b331-af80-45b5-a80b-19c9ea444b2e
* @testdox Four-digit number that is not an Armstrong number
*/
public function testFourDigitIsNotArmstrongNumber(): void
{
$this->assertFalse(isArmstrongNumber(9475));
}

/**
* uuid: f971ced7-8d68-4758-aea1-d4194900b864
* @testdox Seven-digit number that is an Armstrong number
*/
public function testSevenDigitIsArmstrongNumber(): void
{
$this->assertTrue(isArmstrongNumber(9926315));
}

/**
* uuid: 7ee45d52-5d35-4fbd-b6f1-5c8cd8a67f18
* @testdox Seven-digit number that is not an Armstrong number
*/
public function testSevenDigitIsNotArmstrongNumber(): void
{
$this->assertFalse(isArmstrongNumber(9926314));
Expand Down

0 comments on commit abf3d49

Please sign in to comment.