Avatar of GHDIShfdiod876dhs

GHDIShfdiod876dhs's solution

to Triangle in the PHP Track

Published at Jul 04 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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->assertEquals(
            'equilateral',
            (new Triangle(10, 10, 10))->kind()
        );
    }

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

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

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

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

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

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

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

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

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

    public function testTrianglesWithNoSizeAreIllegal()
    {
        $this->expectException(Exception::class);

        (new Triangle(0, 0, 0))->kind();
    }

    public function testTrianglesViolatingTriangleInequalityAreIllegal()
    {
        $this->expectException(Exception::class);

        (new Triangle(1, 1, 3))->kind();
    }

    public function testTrianglesViolatingTriangleInequalityAreIllegal2()
    {
        $this->expectException(Exception::class);

        (new Triangle(7, 3, 2))->kind();
    }

    public function testTrianglesViolatingTriangleInequalityAreIllegal3()
    {
        $this->expectException(Exception::class);

        (new Triangle(1, 3, 1))->kind();
    }
}
<?php

class Triangle
{
  function __construct (float $a, float $b, float $c)
  {
    $sides = [$a, $b, $c];
    sort($sides);
    
    $this->a = $sides[0];
    $this->b = $sides[1];
    $this->c = $sides[2];

    if ($this->a + $this->b < $this->c || $this->a === 0.0 && $this->b === 0.0) {
      throw new Exception();
    }
  }

  function kind(): string
  {
    if ($this->a + $this->b === $this->c) {
      return 'degenerate';
    }
    if ($this->a === $this->b && $this->a === $this->c) {
      return 'equilateral';
    }
    if ($this->a === $this->b  || $this->b === $this->c) {
      return 'isosceles';
    }
    return 'scalene';
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned 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 you could read more about to improve your understanding?