From c0651099acc0ec356417a55490d8392d40043d08 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Sun, 27 Oct 2024 20:12:24 +0100 Subject: [PATCH 1/4] sync collatz conjecture --- .../collatz-conjecture/.docs/instructions.md | 12 +++-- .../collatz-conjecture/.meta/config.json | 2 +- .../collatz-conjecture/.meta/example.php | 24 +-------- .../collatz-conjecture/.meta/tests.toml | 23 +++++++-- .../CollatzConjectureTest.php | 50 ++++++++++--------- 5 files changed, 55 insertions(+), 56 deletions(-) diff --git a/exercises/practice/collatz-conjecture/.docs/instructions.md b/exercises/practice/collatz-conjecture/.docs/instructions.md index f8c76e7f..ba060483 100644 --- a/exercises/practice/collatz-conjecture/.docs/instructions.md +++ b/exercises/practice/collatz-conjecture/.docs/instructions.md @@ -2,10 +2,11 @@ The Collatz Conjecture or 3x+1 problem can be summarized as follows: -Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is -odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely. -The conjecture states that no matter which number you start with, you will -always reach 1 eventually. +Take any positive integer n. +If n is even, divide n by 2 to get n / 2. +If n is odd, multiply n by 3 and add 1 to get 3n + 1. +Repeat the process indefinitely. +The conjecture states that no matter which number you start with, you will always reach 1 eventually. Given a number n, return the number of steps required to reach 1. @@ -24,4 +25,5 @@ Starting with n = 12, the steps would be as follows: 8. 2 9. 1 -Resulting in 9 steps. So for input n = 12, the return value would be 9. +Resulting in 9 steps. +So for input n = 12, the return value would be 9. diff --git a/exercises/practice/collatz-conjecture/.meta/config.json b/exercises/practice/collatz-conjecture/.meta/config.json index bdfec0d2..2c9056eb 100644 --- a/exercises/practice/collatz-conjecture/.meta/config.json +++ b/exercises/practice/collatz-conjecture/.meta/config.json @@ -20,7 +20,7 @@ ".meta/example.php" ] }, - "blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture", + "blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.", "source": "An unsolved problem in mathematics named after mathematician Lothar Collatz", "source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem" } diff --git a/exercises/practice/collatz-conjecture/.meta/example.php b/exercises/practice/collatz-conjecture/.meta/example.php index 502da2d0..ec674458 100644 --- a/exercises/practice/collatz-conjecture/.meta/example.php +++ b/exercises/practice/collatz-conjecture/.meta/example.php @@ -1,34 +1,12 @@ . - * - * To disable strict typing, comment out the directive below. - */ - declare(strict_types=1); function steps($number) { $stepCount = 0; if ($number < 1) { - throw new InvalidArgumentException('Only positive numbers are allowed'); + throw new InvalidArgumentException('Only positive integers are allowed'); } do { if ($number === 1) { diff --git a/exercises/practice/collatz-conjecture/.meta/tests.toml b/exercises/practice/collatz-conjecture/.meta/tests.toml index 04187f60..cc34e168 100644 --- a/exercises/practice/collatz-conjecture/.meta/tests.toml +++ b/exercises/practice/collatz-conjecture/.meta/tests.toml @@ -1,6 +1,13 @@ -# 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. [540a3d51-e7a6-47a5-92a3-4ad1838f0bfd] description = "zero steps for one" @@ -16,6 +23,16 @@ description = "large number of even and odd steps" [7d4750e6-def9-4b86-aec7-9f7eb44f95a3] description = "zero is an error" +include = false + +[2187673d-77d6-4543-975e-66df6c50e2da] +description = "zero is an error" +reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3" [c6c795bf-a288-45e9-86a1-841359ad426d] description = "negative value is an error" +include = false + +[ec11f479-56bc-47fd-a434-bcd7a31a7a2e] +description = "negative value is an error" +reimplements = "c6c795bf-a288-45e9-86a1-841359ad426d" diff --git a/exercises/practice/collatz-conjecture/CollatzConjectureTest.php b/exercises/practice/collatz-conjecture/CollatzConjectureTest.php index 387fd65e..04d8e9fc 100644 --- a/exercises/practice/collatz-conjecture/CollatzConjectureTest.php +++ b/exercises/practice/collatz-conjecture/CollatzConjectureTest.php @@ -1,27 +1,5 @@ . - * - * To disable strict typing, comment out the directive below. - */ - declare(strict_types=1); class CollatzConjectureTest extends PHPUnit\Framework\TestCase @@ -31,38 +9,62 @@ public static function setUpBeforeClass(): void require_once 'CollatzConjecture.php'; } + /** + * uuid: 540a3d51-e7a6-47a5-92a3-4ad1838f0bfd + * @testdox zero steps for one + */ public function testZeroStepsForOne(): void { $this->assertEquals(0, steps(1)); } + /** + * uuid: 3d76a0a6-ea84-444a-821a-f7857c2c1859 + * @testdox divide if even + */ public function testDivideIfEven(): void { $this->assertEquals(4, steps(16)); } + /** + * uuid: 754dea81-123c-429e-b8bc-db20b05a87b9 + * @testdox even and odd steps + */ public function testEvenAndOddSteps(): void { $this->assertEquals(9, steps(12)); } + /** + * uuid: ecfd0210-6f85-44f6-8280-f65534892ff6 + * @testdox large number of even and odd steps + */ public function testLargeNumberOfEvenAndOddSteps(): void { $this->assertEquals(152, steps(1000000)); } + /** + * uuid: 2187673d-77d6-4543-975e-66df6c50e2da + * @testdox zero is an error + */ public function testZeroIsAnError(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Only positive numbers are allowed'); + $this->expectExceptionMessageMatches('/Only positive (numbers|integers) are allowed/'); steps(0); } + /** + * uuid: ec11f479-56bc-47fd-a434-bcd7a31a7a2e + * @testdox negative value is an error + */ public function testNegativeValueIsAnError(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Only positive numbers are allowed'); + $this->expectExceptionMessageMatches('/Only positive (numbers|integers) are allowed/'); steps(-1); } From 01c9641974cddca16e9069fcc02857c0fe274e19 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Wed, 30 Oct 2024 15:15:44 +0100 Subject: [PATCH 2/4] Update exercises/practice/collatz-conjecture/CollatzConjectureTest.php Co-authored-by: mk-mxp <55182845+mk-mxp@users.noreply.github.com> --- exercises/practice/collatz-conjecture/CollatzConjectureTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/collatz-conjecture/CollatzConjectureTest.php b/exercises/practice/collatz-conjecture/CollatzConjectureTest.php index 04d8e9fc..e2d59bef 100644 --- a/exercises/practice/collatz-conjecture/CollatzConjectureTest.php +++ b/exercises/practice/collatz-conjecture/CollatzConjectureTest.php @@ -66,6 +66,6 @@ public function testNegativeValueIsAnError(): void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessageMatches('/Only positive (numbers|integers) are allowed/'); - steps(-1); + steps(-15); } } From 671b727838e03812911ead3491f0e7ca3bdbf39a Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Wed, 30 Oct 2024 15:15:53 +0100 Subject: [PATCH 3/4] Update exercises/practice/collatz-conjecture/CollatzConjectureTest.php Co-authored-by: mk-mxp <55182845+mk-mxp@users.noreply.github.com> --- exercises/practice/collatz-conjecture/CollatzConjectureTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/exercises/practice/collatz-conjecture/CollatzConjectureTest.php b/exercises/practice/collatz-conjecture/CollatzConjectureTest.php index e2d59bef..fe1e1643 100644 --- a/exercises/practice/collatz-conjecture/CollatzConjectureTest.php +++ b/exercises/practice/collatz-conjecture/CollatzConjectureTest.php @@ -64,6 +64,8 @@ public function testZeroIsAnError(): void public function testNegativeValueIsAnError(): void { $this->expectException(InvalidArgumentException::class); + // Matches 'Only positive numbers are allowed' + // or 'Only positive integers are allowed' $this->expectExceptionMessageMatches('/Only positive (numbers|integers) are allowed/'); steps(-15); From 24cc56ce2b8f481ab57e624083df5c04a33a0003 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Wed, 30 Oct 2024 15:16:20 +0100 Subject: [PATCH 4/4] Update exercises/practice/collatz-conjecture/CollatzConjectureTest.php Co-authored-by: mk-mxp <55182845+mk-mxp@users.noreply.github.com> --- exercises/practice/collatz-conjecture/CollatzConjectureTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/exercises/practice/collatz-conjecture/CollatzConjectureTest.php b/exercises/practice/collatz-conjecture/CollatzConjectureTest.php index fe1e1643..ee4c79b0 100644 --- a/exercises/practice/collatz-conjecture/CollatzConjectureTest.php +++ b/exercises/practice/collatz-conjecture/CollatzConjectureTest.php @@ -52,6 +52,8 @@ public function testLargeNumberOfEvenAndOddSteps(): void public function testZeroIsAnError(): void { $this->expectException(InvalidArgumentException::class); + // Matches 'Only positive numbers are allowed' + // or 'Only positive integers are allowed' $this->expectExceptionMessageMatches('/Only positive (numbers|integers) are allowed/'); steps(0);