diff --git a/bin/auto-sync.txt b/bin/auto-sync.txt index 195b074a5..f9d8ab573 100644 --- a/bin/auto-sync.txt +++ b/bin/auto-sync.txt @@ -84,6 +84,7 @@ spiral-matrix state-of-tic-tac-toe strain sublist +triangle twelve-days two-bucket two-fer diff --git a/exercises/practice/triangle/.docs/instructions.md b/exercises/practice/triangle/.docs/instructions.md index 0a9c68e3b..e9b053dcd 100644 --- a/exercises/practice/triangle/.docs/instructions.md +++ b/exercises/practice/triangle/.docs/instructions.md @@ -4,20 +4,32 @@ Determine if a triangle is equilateral, isosceles, or scalene. An _equilateral_ triangle has all three sides the same length. -An _isosceles_ triangle has at least two sides the same length. (It is sometimes -specified as having exactly two sides the same length, but for the purposes of -this exercise we'll say at least two.) +An _isosceles_ triangle has at least two sides the same length. +(It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.) A _scalene_ triangle has all sides of different lengths. ## Note -For a shape to be a triangle at all, all sides have to be of length > 0, and -the sum of the lengths of any two sides must be greater than or equal to the -length of the third side. See [Triangle Inequality](https://en.wikipedia.org/wiki/Triangle_inequality). +For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. -## Dig Deeper +~~~~exercism/note +_Degenerate triangles_ are triangles where the sum of the length of two sides is **equal** to the length of the third side, e.g. `1, 1, 2`. +We opted to not include tests for degenerate triangles in this exercise. +You may handle those situations if you wish to do so, or safely ignore them. +~~~~ -The case where the sum of the lengths of two sides _equals_ that of the -third is known as a _degenerate_ triangle - it has zero area and looks like -a single line. Feel free to add your own code/tests to check for degenerate triangles. +In equations: + +Let `a`, `b`, and `c` be sides of the triangle. +Then all three of the following expressions must be true: + +```text +a + b ≥ c +b + c ≥ a +a + c ≥ b +``` + +See [Triangle Inequality][triangle-inequality] + +[triangle-inequality]: https://en.wikipedia.org/wiki/Triangle_inequality diff --git a/exercises/practice/triangle/.meta/config.json b/exercises/practice/triangle/.meta/config.json index bd56d3f92..3e4b80543 100644 --- a/exercises/practice/triangle/.meta/config.json +++ b/exercises/practice/triangle/.meta/config.json @@ -7,7 +7,8 @@ "G-Rath", "kunicmarko20", "kytrinyx", - "petemcfarlane" + "petemcfarlane", + "Narkunan" ], "files": { "solution": [ @@ -22,5 +23,5 @@ }, "blurb": "Determine if a triangle is equilateral, isosceles, or scalene.", "source": "The Ruby Koans triangle project, parts 1 & 2", - "source_url": "https://rubykoans.com" + "source_url": "https://www.rubykoans.com/" } diff --git a/exercises/practice/triangle/.meta/example.php b/exercises/practice/triangle/.meta/example.php index f9d09a545..8e86ed3a3 100644 --- a/exercises/practice/triangle/.meta/example.php +++ b/exercises/practice/triangle/.meta/example.php @@ -1,27 +1,5 @@ . - * - * To disable strict typing, comment out the directive below. - */ - declare(strict_types=1); class Triangle @@ -29,7 +7,6 @@ class Triangle protected $sideA; protected $sideB; protected $sideC; - public function __construct($sideA, $sideB, $sideC) { $this->sideA = $sideA; @@ -37,33 +14,37 @@ public function __construct($sideA, $sideB, $sideC) $this->sideC = $sideC; } - public function kind(): string + private function isValidTriangle(): bool { - if (0 == ($this->sideA + $this->sideB + $this->sideC)) { - throw new Exception("These sides have no values."); + if ($this->sideA <= 0 || $this->sideB <= 0 || $this->sideC <= 0) { + return false; } - $sides = [$this->sideA, $this->sideB, $this->sideC]; - sort($sides); - if ($sides[2] >= $sides[0] + $sides[1]) { - throw new Exception("This violates the triangle inequality"); - } + return ( + $this->sideA + $this->sideB > $this->sideC && + $this->sideA + $this->sideC > $this->sideB && + $this->sideB + $this->sideC > $this->sideA + ); + } - if ( + public function isEquilateral(): bool + { + return $this->isValidTriangle() && $this->sideA == $this->sideB && - $this->sideA == $this->sideC - ) { - return 'equilateral'; - } + $this->sideA == $this->sideC; + } - if ( - $this->sideB == $this->sideC || + public function isIsosceles(): bool + { + return $this->isValidTriangle() && ( + $this->sideA == $this->sideB || $this->sideA == $this->sideC || - $this->sideA == $this->sideB - ) { - return 'isosceles'; - } + $this->sideB == $this->sideC + ); + } - return 'scalene'; + public function isScalene(): bool + { + return $this->isValidTriangle() && !$this->isIsosceles(); } } diff --git a/exercises/practice/triangle/.meta/tests.toml b/exercises/practice/triangle/.meta/tests.toml index 59107059c..da6346ff5 100644 --- a/exercises/practice/triangle/.meta/tests.toml +++ b/exercises/practice/triangle/.meta/tests.toml @@ -1,60 +1,79 @@ -# 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. [8b2c43ac-7257-43f9-b552-7631a91988af] -description = "all sides are equal" +description = "equilateral triangle -> all sides are equal" [33eb6f87-0498-4ccf-9573-7f8c3ce92b7b] -description = "any side is unequal" +description = "equilateral triangle -> any side is unequal" [c6585b7d-a8c0-4ad8-8a34-e21d36f7ad87] -description = "no sides are equal" +description = "equilateral triangle -> no sides are equal" [16e8ceb0-eadb-46d1-b892-c50327479251] -description = "all zero sides is not a triangle" +description = "equilateral triangle -> all zero sides is not a triangle" [3022f537-b8e5-4cc1-8f12-fd775827a00c] -description = "sides may be floats" +description = "equilateral triangle -> sides may be floats" +include = false +comment = "comparing floats would make the exercise too difficult" [cbc612dc-d75a-4c1c-87fc-e2d5edd70b71] -description = "last two sides are equal" +description = "isosceles triangle -> last two sides are equal" [e388ce93-f25e-4daf-b977-4b7ede992217] -description = "first two sides are equal" +description = "isosceles triangle -> first two sides are equal" [d2080b79-4523-4c3f-9d42-2da6e81ab30f] -description = "first and last sides are equal" +description = "isosceles triangle -> first and last sides are equal" [8d71e185-2bd7-4841-b7e1-71689a5491d8] -description = "equilateral triangles are also isosceles" +description = "isosceles triangle -> equilateral triangles are also isosceles" [840ed5f8-366f-43c5-ac69-8f05e6f10bbb] -description = "no sides are equal" +description = "isosceles triangle -> no sides are equal" [2eba0cfb-6c65-4c40-8146-30b608905eae] -description = "first triangle inequality violation" +description = "isosceles triangle -> first triangle inequality violation" [278469cb-ac6b-41f0-81d4-66d9b828f8ac] -description = "second triangle inequality violation" +description = "isosceles triangle -> second triangle inequality violation" [90efb0c7-72bb-4514-b320-3a3892e278ff] -description = "third triangle inequality violation" +description = "isosceles triangle -> third triangle inequality violation" [adb4ee20-532f-43dc-8d31-e9271b7ef2bc] -description = "sides may be floats" +description = "isosceles triangle -> sides may be floats" +include = false +comment = "comparing floats would make the exercise too difficult" [e8b5f09c-ec2e-47c1-abec-f35095733afb] -description = "no sides are equal" +description = "scalene triangle -> no sides are equal" [2510001f-b44d-4d18-9872-2303e7977dc1] -description = "all sides are equal" +description = "scalene triangle -> all sides are equal" [c6e15a92-90d9-4fb3-90a2-eef64f8d3e1e] -description = "two sides are equal" +description = "scalene triangle -> first and second sides are equal" + +[3da23a91-a166-419a-9abf-baf4868fd985] +description = "scalene triangle -> first and third sides are equal" + +[b6a75d98-1fef-4c42-8e9a-9db854ba0a4d] +description = "scalene triangle -> second and third sides are equal" [70ad5154-0033-48b7-af2c-b8d739cd9fdc] -description = "may not violate triangle inequality" +description = "scalene triangle -> may not violate triangle inequality" [26d9d59d-f8f1-40d3-ad58-ae4d54123d7d] -description = "sides may be floats" +description = "scalene triangle -> sides may be floats" +include = false +comment = "comparing floats would make the exercise too difficult" diff --git a/exercises/practice/triangle/Triangle.php b/exercises/practice/triangle/Triangle.php index 85fde79f9..e1eec545e 100644 --- a/exercises/practice/triangle/Triangle.php +++ b/exercises/practice/triangle/Triangle.php @@ -28,11 +28,21 @@ class Triangle { public function __construct(int $a, int $b, int $c) { - throw new \BadMethodCallException("Implement the __construct method"); + throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } - public function kind(): string + public function isEquilateral(): bool { - throw new \BadMethodCallException("Implement the kind method"); + throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); + } + + public function isIsosceles(): bool + { + throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); + } + + public function isScalene(): bool + { + throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } } diff --git a/exercises/practice/triangle/TriangleTest.php b/exercises/practice/triangle/TriangleTest.php index 4db1ef1bd..7bfb1f4e2 100644 --- a/exercises/practice/triangle/TriangleTest.php +++ b/exercises/practice/triangle/TriangleTest.php @@ -1,29 +1,8 @@ . - * - * To disable strict typing, comment out the directive below. - */ - declare(strict_types=1); +use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; class TriangleTest extends TestCase @@ -33,119 +12,195 @@ public static function setUpBeforeClass(): void require_once 'Triangle.php'; } + /** + * Uuid: 8b2c43ac-7257-43f9-b552-7631a91988af. + */ + #[TestDox('equilateral triangle -> all sides are equal')] public function testEquilateralTrianglesHaveEqualSides(): void { - $this->assertEquals( - 'equilateral', - (new Triangle(2, 2, 2))->kind() + $this->assertTrue( + (new Triangle(2, 2, 2))->isEquilateral() ); } - public function testLargerEquilateralTrianglesHaveEqualSides(): void + /** + * Uuid: 33eb6f87-0498-4ccf-9573-7f8c3ce92b7b. + */ + #[TestDox('equilateral triangle -> any side is unequal')] + public function testAnySideIsUnequal(): void { - $this->assertEquals( - 'equilateral', - (new Triangle(10, 10, 10))->kind() + + $this->assertFalse( + (new Triangle(2, 3, 2))->isEquilateral() ); } - public function testIsoscelesTriangleWhenLastTwoSidesAreEqual(): void + /** + * Uuid: c6585b7d-a8c0-4ad8-8a34-e21d36f7ad87. + */ + #[TestDox('equilateral triangle -> no sides are equal')] + public function testNoSidesAreEqual(): void { - $this->assertEquals( - 'isosceles', - (new Triangle(3, 4, 4))->kind() + $this->assertFalse( + (new Triangle(5, 4, 6))->isEquilateral() ); } - public function testIsoscelesTriangleWhenFirstAndLastSidesAreEqual(): void + /** + * Uuid: 16e8ceb0-eadb-46d1-b892-c50327479251. + */ + #[TestDox('equilateral triangle -> all zero sides is not a triangle')] + public function testEquilateralAllZeroSidesIsNotATriangle(): void + { + + $this->assertFalse( + (new Triangle(0, 0, 0))->isEquilateral() + ); + } + + /** + * Uuid: cbc612dc-d75a-4c1c-87fc-e2d5edd70b71. + */ + #[TestDox('isosceles triangle -> last two sides are equal')] + public function testIsoscelesTriangleWhenLastTwoSidesAreEqual(): void { - $this->assertEquals( - 'isosceles', - (new Triangle(4, 3, 4))->kind() + $this->assertTrue( + (new Triangle(3, 4, 4))->isIsosceles() ); } + /** + * Uuid: e388ce93-f25e-4daf-b977-4b7ede992217. + */ + #[TestDox('isosceles triangle -> first two sides are equal')] public function testIsoscelesTriangleWhenFirstTwoSidesAreEqual(): void { - $this->assertEquals( - 'isosceles', - (new Triangle(4, 4, 3))->kind() + $this->assertTrue( + (new Triangle(4, 4, 3))->isIsosceles() ); } - public function testIsoscelesTrianglesWithUnequalSideLargerThanEqualSides(): void + /** + * Uuid: d2080b79-4523-4c3f-9d42-2da6e81ab30f. + */ + #[TestDox('isosceles triangle -> first and last sides are equal')] + public function testIsoscelesTriangleWhenFirstAndLastSidesAreEqual(): void { - $this->assertEquals( - 'isosceles', - (new Triangle(4, 4, 7))->kind() + $this->assertTrue( + (new Triangle(4, 3, 4))->isIsosceles() ); } - public function testScaleneTrianglesHaveNoEqualSides(): void + /** + * Uuid: 8d71e185-2bd7-4841-b7e1-71689a5491d8. + */ + #[TestDox('isosceles triangle -> equilateral triangles are also isosceles')] + public function testIsoscelesTriangleEquilateralTrianglesAreAlsoIsoceles(): void { - $this->assertEquals( - 'scalene', - (new Triangle(3, 4, 5))->kind() + $this->assertTrue( + (new Triangle(4, 4, 4))->isIsosceles() ); } - public function test2aEqualsBPlusCLooksLikeEquilateralButIsNot(): void + /** + * Uuid: 840ed5f8-366f-43c5-ac69-8f05e6f10bbb. + */ + #[TestDox('isosceles triangle -> no sides are equal')] + public function testIsoscelesTriangleNoSidesAreEqual(): void { - $this->assertEquals( - 'scalene', - (new Triangle(5, 4, 6))->kind() + $this->assertFalse( + (new Triangle(2, 3, 4))->isIsosceles() ); } - public function testScaleneTrianglesHaveNoEqualSidesAtLargerScale(): void + /** + * Uuid: 2eba0cfb-6c65-4c40-8146-30b608905eae. + */ + #[TestDox('isosceles triangle -> first triangle inequality violation')] + public function testIsoscelesFirstTriangleInequalityViolation(): void { - $this->assertEquals( - 'scalene', - (new Triangle(10, 11, 12))->kind() + + $this->assertFalse( + (new Triangle(1, 1, 3))->isIsosceles() ); } - public function testScaleneTrianglesHaveNoEqualSidesInDescendingOrder(): void + /** + * Uuid: 278469cb-ac6b-41f0-81d4-66d9b828f8ac. + */ + #[TestDox('isosceles triangle -> second triangle inequality violation')] + public function testIsoscelesSecondTriangleInequalityViolation(): void { - $this->assertEquals( - 'scalene', - (new Triangle(5, 4, 2))->kind() + $this->assertFalse( + (new Triangle(1, 3, 1))->isIsosceles() ); } - public function testVerySmallTrianglesAreLegal(): void + /** + * Uuid: 90efb0c7-72bb-4514-b320-3a3892e278ff. + */ + #[TestDox('isosceles triangle -> third triangle inequality violation')] + public function testTrianglesThirdTriangleInequalityViolation(): void { - $this->assertEquals( - 'scalene', - (new Triangle(0.4, 0.6, 0.3))->kind() + $this->assertFalse( + (new Triangle(3, 1, 1))->isIsosceles() ); } - public function testTrianglesWithNoSizeAreIllegal(): void + /** + * Uuid: 2510001f-b44d-4d18-9872-2303e7977dc1. + */ + #[TestDox('scalene triangle -> all sides are equal')] + public function testScaleneAllSidesAreEqual(): void { - $this->expectException(Exception::class); - (new Triangle(0, 0, 0))->kind(); + $this->assertFalse( + (new Triangle(4, 4, 4))->isScalene() + ); } - public function testTrianglesViolatingTriangleInequalityAreIllegal(): void + /** + * Uuid: c6e15a92-90d9-4fb3-90a2-eef64f8d3e1e. + */ + #[TestDox('scalene triangle -> first and second sides are equal')] + public function testScaleneFirstAndSecondSidesAreEqual(): void { - $this->expectException(Exception::class); - (new Triangle(1, 1, 3))->kind(); + $this->assertFalse( + (new Triangle(4, 4, 3))->isScalene() + ); } - public function testTrianglesViolatingTriangleInequalityAreIllegal2(): void + /** + * Uuid: 3da23a91-a166-419a-9abf-baf4868fd985. + */ + #[TestDox('scalene triangle -> first and third sides are equal')] + public function testScaleneFirstAndThirdSidesAreEqual(): void { - $this->expectException(Exception::class); - - (new Triangle(7, 3, 2))->kind(); + $this->assertFalse( + (new Triangle(3, 4, 3))->isScalene() + ); } - public function testTrianglesViolatingTriangleInequalityAreIllegal3(): void + /** + * Uuid: b6a75d98-1fef-4c42-8e9a-9db854ba0a4d. + */ + #[TestDox('scalene triangle -> second and third sides are equal')] + public function testScaleneSecondAndThirdSidesAreEqual(): void { - $this->expectException(Exception::class); + $this->assertFalse( + (new Triangle(4, 3, 3))->isScalene() + ); + } - (new Triangle(1, 3, 1))->kind(); + /** + * Uuid: 70ad5154-0033-48b7-af2c-b8d739cd9fdc. + */ + #[TestDox('scalene triangle -> may not violate triangle inequality')] + public function testScaleneMayNotViolateTriangleInequality(): void + { + $this->assertFalse( + (new Triangle(7, 3, 2))->isScalene() + ); } }