Avatar of 4d47
0
0
Genius
0
1

4d47's solution

to Triangle in the PHP Track

0
0
Genius
0
1
Instructions
Test suite
Solution

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.)

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.

Dig Deeper

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.

Running the tests

  1. Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php. To find the Exercism workspace run

     % exercism debug | grep Workspace
    
  2. Get PHPUnit if you don't have it already.

     % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
     % chmod +x phpunit.phar
    
  3. Execute the tests:

     % ./phpunit.phar triangle/triangle_test.php
    

Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

triangle_test.php

<?php

require "triangle.php";

class TriangleTest extends PHPUnit\Framework\TestCase
{
    public function testEquilateralTrianglesHaveEqualSides()
    {
        $this->assertEquals(
            'equilateral',
            (new Triangle(2, 2, 2))->kind()
        );
    }

    public function testLargerEquilateralTrianglesHaveEqualSides()
    {
        $this->markTestSkipped();
        $this->assertEquals(
            'equilateral',
            (new Triangle(10, 10, 10))->kind()
        );
    }

    public function testIsoscelesTriangleWhenLastTwoSidesAreEqual()
    {
        $this->markTestSkipped();
        $this->assertEquals(
            'isosceles',
            (new Triangle(3, 4, 4))->kind()
        );
    }

    public function testIsoscelesTriangleWhenFirstAndLastSidesAreEqual()
    {
        $this->markTestSkipped();
        $this->assertEquals(
            'isosceles',
            (new Triangle(4, 3, 4))->kind()
        );
    }

    public function testIsoscelesTriangleWhenFirstTwoSidesAreEqual()
    {
        $this->markTestSkipped();
        $this->assertEquals(
            'isosceles',
            (new Triangle(4, 4, 3))->kind()
        );
    }

    public function testIsoscelesTrianglesWithUnequalSideLargerThanEqualSides()
    {
        $this->markTestSkipped();
        $this->assertEquals(
            'isosceles',
            (new Triangle(4, 4, 7))->kind()
        );
    }

    public function testScaleneTrianglesHaveNoEqualSides()
    {
        $this->markTestSkipped();
        $this->assertEquals(
            'scalene',
            (new Triangle(3, 4, 5))->kind()
        );
    }

    public function test2aEqualsBPlusCLooksLikeEquilateralButIsNot()
    {
        $this->markTestSkipped();
        $this->assertEquals(
            'scalene',
            (new Triangle(5, 4, 6))->kind()
        );
    }

    public function testScaleneTrianglesHaveNoEqualSidesAtLargerScale()
    {
        $this->markTestSkipped();
        $this->assertEquals(
            'scalene',
            (new Triangle(10, 11, 12))->kind()
        );
    }

    public function testScaleneTrianglesHaveNoEqualSidesInDescendingOrder()
    {
        $this->markTestSkipped();
        $this->assertEquals(
            'scalene',
            (new Triangle(5, 4, 2))->kind()
        );
    }

    public function testVerySmallTrianglesAreLegal()
    {
        $this->markTestSkipped();
        $this->assertEquals(
            'scalene',
            (new Triangle(0.4, 0.6, 0.3))->kind()
        );
    }

    /**
     * @expectedException \Exception
     */
    public function testTrianglesWithNoSizeAreIllegal()
    {
        $this->markTestSkipped();
        (new Triangle(0, 0, 0))->kind();
    }

    /**
     * @expectedException \Exception
     */
    public function testTrianglesViolatingTriangleInequalityAreIllegal()
    {
        $this->markTestSkipped();
        (new Triangle(1, 1, 3))->kind();
    }

    /**
     * @expectedException \Exception
     */
    public function testTrianglesViolatingTriangleInequalityAreIllegal2()
    {
        $this->markTestSkipped();
        (new Triangle(7, 3, 2))->kind();
    }

    /**
     * @expectedException \Exception
     */
    public function testTrianglesViolatingTriangleInequalityAreIllegal3()
    {
        $this->markTestSkipped();
        (new Triangle(1, 3, 1))->kind();
    }
}
<?php

class Triangle {
    private $kind;

    public function __construct(float $x, float $y, float $z) {
        if (min($x, $y, $z) <= 0) {
            throw new InvalidArgumentException('All sides must be positive');
        }
        if (!($x + $y >= $z && $x + $z >= $y && $y + $z >= $x)) {
            throw new InvalidArgumentException('the sum of any two sides must be greater than or equal to the length of the third side');
        }
        if ($x == $y && $x == $z) {
            $this->kind = 'equilateral';
        } elseif ($x == $y || $x == $z || $y == $z) {
            $this->kind = 'isosceles';
        } else {
            $this->kind = 'scalene';
        }
    }

    public function kind(): string {
        return $this->kind;
    }
}

What can you learn from this solution?

A huge amount can be learnt from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that I could read more about to develop my understanding?

Community comments

See what others have said about this solution
over 1 year ago
4d47 says

Learned that when you have data that relates it's often better to put them together in proper data structure.