From b91feb76ff86703dee16ff7d2c72d75ecbafed10 Mon Sep 17 00:00:00 2001 From: resu-xuniL Date: Wed, 1 Jul 2026 14:42:06 +0200 Subject: [PATCH 1/7] Add `square-root` exercice --- config.json | 8 +++ .../square-root/.docs/instructions.md | 18 +++++ .../square-root/.docs/introduction.md | 10 +++ .../practice/square-root/.meta/config.json | 19 ++++++ .../practice/square-root/.meta/example.php | 14 ++++ .../practice/square-root/.meta/tests.toml | 28 ++++++++ exercises/practice/square-root/SquareRoot.php | 30 ++++++++ .../practice/square-root/SquareRootTest.php | 68 +++++++++++++++++++ 8 files changed, 195 insertions(+) create mode 100644 exercises/practice/square-root/.docs/instructions.md create mode 100644 exercises/practice/square-root/.docs/introduction.md create mode 100644 exercises/practice/square-root/.meta/config.json create mode 100644 exercises/practice/square-root/.meta/example.php create mode 100644 exercises/practice/square-root/.meta/tests.toml create mode 100644 exercises/practice/square-root/SquareRoot.php create mode 100644 exercises/practice/square-root/SquareRootTest.php diff --git a/config.json b/config.json index 56444532..478afd6c 100644 --- a/config.json +++ b/config.json @@ -489,6 +489,14 @@ "floating_point_numbers" ] }, + { + "slug": "square-root", + "name": "Square Root", + "uuid": "d75c009b-ad65-455f-a824-ed61c8797cc4", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, { "slug": "transpose", "name": "Transpose", diff --git a/exercises/practice/square-root/.docs/instructions.md b/exercises/practice/square-root/.docs/instructions.md new file mode 100644 index 00000000..d258b868 --- /dev/null +++ b/exercises/practice/square-root/.docs/instructions.md @@ -0,0 +1,18 @@ +# Instructions + +Your task is to calculate the square root of a given number. + +- Try to avoid using the pre-existing math libraries of your language. +- As input you'll be given a positive whole number, i.e. 1, 2, 3, 4… +- You are only required to handle cases where the result is a positive whole number. + +Some potential approaches: + +- Linear or binary search for a number that gives the input number when squared. +- Successive approximation using Newton's or Heron's method. +- Calculating one digit at a time or one bit at a time. + +You can check out the Wikipedia pages on [integer square root][integer-square-root] and [methods of computing square roots][computing-square-roots] to help with choosing a method of calculation. + +[integer-square-root]: https://en.wikipedia.org/wiki/Integer_square_root +[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots diff --git a/exercises/practice/square-root/.docs/introduction.md b/exercises/practice/square-root/.docs/introduction.md new file mode 100644 index 00000000..1d692934 --- /dev/null +++ b/exercises/practice/square-root/.docs/introduction.md @@ -0,0 +1,10 @@ +# Introduction + +We are launching a deep space exploration rocket and we need a way to make sure the navigation system stays on target. + +As the first step in our calculation, we take a target number and find its square root (that is, the number that when multiplied by itself equals the target number). + +The journey will be very long. +To make the batteries last as long as possible, we had to make our rocket's onboard computer very power efficient. +Unfortunately that means that we can't rely on fancy math libraries and functions, as they use more power. +Instead we want to implement our own square root calculation. diff --git a/exercises/practice/square-root/.meta/config.json b/exercises/practice/square-root/.meta/config.json new file mode 100644 index 00000000..05869605 --- /dev/null +++ b/exercises/practice/square-root/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "resu-xuniL" + ], + "files": { + "solution": [ + "SquareRoot.php" + ], + "test": [ + "SquareRootTest.php" + ], + "example": [ + ".meta/example.php" + ] + }, + "blurb": "Given a natural radicand, return its square root.", + "source": "wolf99", + "source_url": "https://github.com/exercism/problem-specifications/pull/1582" +} diff --git a/exercises/practice/square-root/.meta/example.php b/exercises/practice/square-root/.meta/example.php new file mode 100644 index 00000000..76f17305 --- /dev/null +++ b/exercises/practice/square-root/.meta/example.php @@ -0,0 +1,14 @@ + $number) { + $sqrt--; + } + + return $sqrt; +} diff --git a/exercises/practice/square-root/.meta/tests.toml b/exercises/practice/square-root/.meta/tests.toml new file mode 100644 index 00000000..ead7882f --- /dev/null +++ b/exercises/practice/square-root/.meta/tests.toml @@ -0,0 +1,28 @@ +# 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. + +[9b748478-7b0a-490c-b87a-609dacf631fd] +description = "root of 1" + +[7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb] +description = "root of 4" + +[6624aabf-3659-4ae0-a1c8-25ae7f33c6ef] +description = "root of 25" + +[93beac69-265e-4429-abb1-94506b431f81] +description = "root of 81" + +[fbddfeda-8c4f-4bc4-87ca-6991af35360e] +description = "root of 196" + +[c03d0532-8368-4734-a8e0-f96a9eb7fc1d] +description = "root of 65025" diff --git a/exercises/practice/square-root/SquareRoot.php b/exercises/practice/square-root/SquareRoot.php new file mode 100644 index 00000000..a12efa02 --- /dev/null +++ b/exercises/practice/square-root/SquareRoot.php @@ -0,0 +1,30 @@ +. + * + * To disable strict typing, comment out the directive below. + */ + +declare(strict_types=1); + +function squareRoot(int $number): int +{ + throw new \BadMethodCallException(sprintf('Implement the %s function', __FUNCTION__)); +} diff --git a/exercises/practice/square-root/SquareRootTest.php b/exercises/practice/square-root/SquareRootTest.php new file mode 100644 index 00000000..3644d98e --- /dev/null +++ b/exercises/practice/square-root/SquareRootTest.php @@ -0,0 +1,68 @@ +assertEquals(1, squareRoot(1)); + } + + /** + * uuid: 7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb + */ + #[TestDox('root of 4')] + public function testRootOfFour(): void + { + $this->assertEquals(2, squareRoot(4)); + } + + /** + * uuid: 6624aabf-3659-4ae0-a1c8-25ae7f33c6ef + */ + #[TestDox('root of 25')] + public function testRootOfTwentyFive(): void + { + $this->assertEquals(5, squareRoot(25)); + } + + /** + * uuid: 93beac69-265e-4429-abb1-94506b431f81 + */ + #[TestDox('root of 81')] + public function testRootOfEightyOne(): void + { + $this->assertEquals(9, squareRoot(81)); + } + + /** + * uuid: fbddfeda-8c4f-4bc4-87ca-6991af35360e + */ + #[TestDox('root of 196')] + public function testRootOfOneHundredNinetySix(): void + { + $this->assertEquals(14, squareRoot(196)); + } + + /** + * uuid: c03d0532-8368-4734-a8e0-f96a9eb7fc1d + */ + #[TestDox('root of 65025')] + public function testRootOfSixtyFiveThousandTwentyFive(): void + { + $this->assertEquals(255, squareRoot(65025)); + } +} From b9741ff22a32609e3372e80b546c1bb5a139bc13 Mon Sep 17 00:00:00 2001 From: resu-xuniL Date: Thu, 2 Jul 2026 08:29:08 +0200 Subject: [PATCH 2/7] Update `auto-sync.txt` and add exercice --- bin/auto-sync.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/auto-sync.txt b/bin/auto-sync.txt index 3d2c7dfb..6143429b 100644 --- a/bin/auto-sync.txt +++ b/bin/auto-sync.txt @@ -29,6 +29,7 @@ etl flatten-array flower-field food-chain +game-of-life gigasecond grade-school grains @@ -68,6 +69,7 @@ rail-fence-cipher raindrops resistor-color resistor-color-duo +resistor-color-trio reverse-string rna-transcription robot-name @@ -83,6 +85,7 @@ series sieve space-age spiral-matrix +square-root state-of-tic-tac-toe strain sublist From 01a06e99e96d2573a3a1868d296a8c2b4bde5ecc Mon Sep 17 00:00:00 2001 From: resu-xuniL Date: Thu, 2 Jul 2026 20:57:07 +0200 Subject: [PATCH 3/7] Change difficulty rating --- config.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/config.json b/config.json index 478afd6c..f9d90050 100644 --- a/config.json +++ b/config.json @@ -296,14 +296,6 @@ "prerequisites": [], "difficulty": 1 }, - { - "slug": "crypto-square", - "name": "Crypto Square", - "uuid": "14aa815d-8073-4a71-841e-9bfdba4b0b1a", - "practices": [], - "prerequisites": [], - "difficulty": 1 - }, { "slug": "diamond", "name": "Diamond", @@ -585,6 +577,14 @@ "math" ] }, + { + "slug": "crypto-square", + "name": "Crypto Square", + "uuid": "14aa815d-8073-4a71-841e-9bfdba4b0b1a", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "darts", "name": "Darts", From a9074c13907d117a4ae46d398905d2fc1f50a495 Mon Sep 17 00:00:00 2001 From: resu-xuniL Date: Thu, 2 Jul 2026 20:58:06 +0200 Subject: [PATCH 4/7] Add test "not use or mention PHP square root functions" --- exercises/practice/square-root/SquareRootTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/exercises/practice/square-root/SquareRootTest.php b/exercises/practice/square-root/SquareRootTest.php index 3644d98e..8d741692 100644 --- a/exercises/practice/square-root/SquareRootTest.php +++ b/exercises/practice/square-root/SquareRootTest.php @@ -12,6 +12,14 @@ public static function setUpBeforeClass(): void require_once 'SquareRoot.php'; } + #[TestDox('does not use or mention PHP square root functions')] + public function testDoesNotUseOrMentionPhpSquareRootFunctions(): void + { + $code = file_get_contents('SquareRoot.php'); + + $this->assertStringNotContainsString('sqrt(', $code); + } + /** * uuid: 9b748478-7b0a-490c-b87a-609dacf631fd */ From 9418979bc431f2d27b789d0143b59b6dd7cc33b9 Mon Sep 17 00:00:00 2001 From: resu-xuniL Date: Thu, 2 Jul 2026 21:06:30 +0200 Subject: [PATCH 5/7] Fix path --- exercises/practice/square-root/SquareRootTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/square-root/SquareRootTest.php b/exercises/practice/square-root/SquareRootTest.php index 8d741692..486402ff 100644 --- a/exercises/practice/square-root/SquareRootTest.php +++ b/exercises/practice/square-root/SquareRootTest.php @@ -15,7 +15,7 @@ public static function setUpBeforeClass(): void #[TestDox('does not use or mention PHP square root functions')] public function testDoesNotUseOrMentionPhpSquareRootFunctions(): void { - $code = file_get_contents('SquareRoot.php'); + $code = file_get_contents(__DIR__ . '/SquareRoot.php'); $this->assertStringNotContainsString('sqrt(', $code); } From 9ffb85435b7053845426e006c13be8b82bea11e1 Mon Sep 17 00:00:00 2001 From: resu-xuniL Date: Fri, 3 Jul 2026 19:10:30 +0200 Subject: [PATCH 6/7] Add message for assertion validation --- exercises/practice/square-root/.meta/example.php | 8 ++++---- exercises/practice/square-root/SquareRootTest.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/exercises/practice/square-root/.meta/example.php b/exercises/practice/square-root/.meta/example.php index 76f17305..46e79fcd 100644 --- a/exercises/practice/square-root/.meta/example.php +++ b/exercises/practice/square-root/.meta/example.php @@ -4,11 +4,11 @@ function squareRoot(int $number): int { - $sqrt = $number; + $squareRoot = $number; - while (pow($sqrt, 2) > $number) { - $sqrt--; + while (pow($squareRoot, 2) > $number) { + $squareRoot--; } - return $sqrt; + return $squareRoot; } diff --git a/exercises/practice/square-root/SquareRootTest.php b/exercises/practice/square-root/SquareRootTest.php index 486402ff..ef66b769 100644 --- a/exercises/practice/square-root/SquareRootTest.php +++ b/exercises/practice/square-root/SquareRootTest.php @@ -17,7 +17,7 @@ public function testDoesNotUseOrMentionPhpSquareRootFunctions(): void { $code = file_get_contents(__DIR__ . '/SquareRoot.php'); - $this->assertStringNotContainsString('sqrt(', $code); + $this->assertStringNotContainsString('sqrt', $code, 'Please do not use the word "sqrt" anywhere in your code!'); } /** From 3a6caae76ef0881f8231b544cc45c0151ff0dac2 Mon Sep 17 00:00:00 2001 From: resu-xuniL Date: Fri, 3 Jul 2026 19:35:05 +0200 Subject: [PATCH 7/7] Fix `config.json` --- config.json | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/config.json b/config.json index f9d90050..13f89f4a 100644 --- a/config.json +++ b/config.json @@ -296,6 +296,14 @@ "prerequisites": [], "difficulty": 1 }, + { + "slug": "crypto-square", + "name": "Crypto Square", + "uuid": "14aa815d-8073-4a71-841e-9bfdba4b0b1a", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, { "slug": "diamond", "name": "Diamond", @@ -481,14 +489,6 @@ "floating_point_numbers" ] }, - { - "slug": "square-root", - "name": "Square Root", - "uuid": "d75c009b-ad65-455f-a824-ed61c8797cc4", - "practices": [], - "prerequisites": [], - "difficulty": 1 - }, { "slug": "transpose", "name": "Transpose", @@ -577,14 +577,6 @@ "math" ] }, - { - "slug": "crypto-square", - "name": "Crypto Square", - "uuid": "14aa815d-8073-4a71-841e-9bfdba4b0b1a", - "practices": [], - "prerequisites": [], - "difficulty": 2 - }, { "slug": "darts", "name": "Darts", @@ -736,6 +728,14 @@ "strings" ] }, + { + "slug": "square-root", + "name": "Square Root", + "uuid": "d75c009b-ad65-455f-a824-ed61c8797cc4", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "strain", "name": "Strain",