🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of webmandman

webmandman's solution

to Triangle in the CFML Track

Published at Dec 11 2018 · 0 comments
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.


To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, cd into the exercise folder and run the following:

box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

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.

SolutionTest.cfc

component extends="TriangleTest" {

	function beforeAll(){
	  SUT = createObject( 'Solution' );
	}

}

TriangleTest.cfc

component extends="testbox.system.BaseSpec" {

	function beforeAll(){
	  SUT = createObject( 'Triangle' );
	}

	function run(){
	
		describe( "My Triangle class", function(){			

			describe( 'returns true if the triangle is equilateral', function(){

				it( 'true if all sides are equal', function(){
					expect( SUT.equilateral( sides=[2, 2, 2] ) ).toBeTrue();
				});

				it( 'false if any side is unequal', function(){
					expect( SUT.equilateral( sides=[2, 3, 2] ) ).toBeFalse();
				});

				it( 'false if no sides are equal', function(){
					expect( SUT.equilateral( sides=[5, 4, 6] ) ).toBeFalse();
				});

				it( 'All zero sides are illegal, so the triangle is not equilateral', function(){
					expect( SUT.equilateral( sides=[0, 0, 0] ) ).toBeFalse();
				});

				it( 'sides may be floats', function(){
					expect( SUT.equilateral( sides=[0.5, 0.5, 0.5] ) ).toBeTrue();
				});

			});

			describe( 'returns true if the triangle is isosceles', function(){

				it( 'true if last two sides are equal', function(){
					expect( SUT.isosceles( sides=[3, 4, 4] ) ).toBeTrue();
				});

				it( 'true if first two sides are equal', function(){
					expect( SUT.isosceles( sides=[4, 4, 3] ) ).toBeTrue();
				});

				it( 'true if first and last sides are equal', function(){
					expect( SUT.isosceles( sides=[4, 3, 4] ) ).toBeTrue();
				});

				it( 'equilateral triangles are also isosceles', function(){
					expect( SUT.isosceles( sides=[4, 4, 4] ) ).toBeTrue();
				});

				it( 'false if no sides are equal', function(){
					expect( SUT.isosceles( sides=[2, 3, 4] ) ).toBeFalse();
				});

				it( 'Sides that violate triangle inequality are not isosceles, even if two are equal', function(){
					expect( SUT.isosceles( sides=[1, 1, 3] ) ).toBeFalse();
				});

				it( 'sides may be floats', function(){
					expect( SUT.isosceles( sides=[0.5, 0.4, 0.5] ) ).toBeTrue();
				});

			});

			describe( 'returns true if the triangle is scalene', function(){

				it( 'true if no sides are equal', function(){
					expect( SUT.scalene( sides=[5, 4, 6] ) ).toBeTrue();
				});

				it( 'false if all sides are equal', function(){
					expect( SUT.scalene( sides=[4, 4, 4] ) ).toBeFalse();
				});

				it( 'false if two sides are equal', function(){
					expect( SUT.scalene( sides=[4, 4, 3] ) ).toBeFalse();
				});

				it( 'Sides that violate triangle inequality are not scalene, even if they are all different', function(){
					expect( SUT.scalene( sides=[7, 3, 2] ) ).toBeFalse();
				});

				it( 'sides may be floats', function(){
					expect( SUT.scalene( sides=[0.5, 0.4, 0.6] ) ).toBeTrue();
				});

			});

		});
		
	}
 
}
/**
* Your implmentation of the Triangle exercise
*/
component {

	/**
	* @returns 
	*/
	function equilateral( array sides ) {
		if(not isTriangle(sides)) return false;

		if(identicalSides(sides) gt 1) return true;
		return false;
	}
	
	function isosceles( array sides ) {
		if(not isTriangle(sides)) return false;

		if(identicalSides(sides) gt 0) return true;
		return false;
	}
	
	function scalene( array sides ) {
		if(not isTriangle(sides)) return false;

		if(identicalSides(sides) eq 0 ) return true;
		return false;
	}

	function degenerate( array sides ) {
		if(not isTriangle(sides)) return false;

		var a = sides[1];
		var b = sides[2];
		var c = sides[3];

		if(a+b eq c) return true;
		if(a+c eq b) return true;
		if(b+c eq a) return true;

		return false;
	}
	
	private function isTriangle(sides){
		// if all sides are zero, not a triangle
		if(sides.sum() eq 0) return false;
		// has 3 sides?
		if(sides.len() neq 3) return false;
		// sum of two sides must be greater than 3rd side
		var a = sides[1];
		var b = sides[2];
		var c = sides[3];
		if(a+b lt c) return false;
		if(a+c lt b) return false;
		if(b+c lt a) return false;
		// its a triangle!
		return true;
	}

	private function identicalSides(sides){
		var a = sides[1];
		var b = sides[2];
		var c = sides[3];

		var identicalSides = 0;

		if(a == b) identicalSides++;
		if(a == c) identicalSides++;
		if(b == c) identicalSides++;

		return identicalSides;
	}
}

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?