Sha256: ec497b99e8c5921dc2078d3963e2e80034b06da8f813c6ea6521434ffd7c96f6

Contents?: true

Size: 1.31 KB

Versions: 146

Compression:

Stored size: 1.31 KB

Contents

/**
* Here is an example solution for the Triangle exercise
*/
component {
	
	function equilateral( array sides ) {
		// Is triangle and all 3 sides the same
		return isTriangle( sides ) && countSides( sides ) == 1;
	}
	
	function isosceles( array sides ) {
		// Is triangle and at least two sides the same
		return isTriangle( sides ) && countSides( sides ) < 3;
	}
	
	function scalene( array sides ) {
		// Is triangle and all 3 sides different
		return isTriangle( sides ) && countSides( sides ) == 3;
	}
	
	private function countSides( array sides ) {
		var distinctSides = {};
				
		// Keep track of distinct sides
		sides.each( function( side ){
			distinctSides[ side ] = true;
		} );
		
		// Count 'em up
		return distinctSides.count();
	}
	
	private function isTriangle( array sides ) {
		// Any sides of 0 are invalid
		if( sides[ 1 ] == 0 || sides[ 2 ] == 0 || sides[ 3 ] == 0 ) {
			return false;
		}
		// sides 1 and 2 must be at least the size of side 3
		if( sides[ 1 ] + sides[ 2 ] < sides[ 3 ] ) {
			return false;
		}
		// sides 1 and 3 must be at least the size of side 2
		if( sides[ 1 ] + sides[ 3 ] < sides[ 2 ] ) {
			return false;
		}
		// sides 2 and 3 must be at least the size of side 1
		if( sides[ 2 ] + sides[ 3 ] < sides[ 1 ] ) {
			return false;
		}
		// If we made it here, it's a triangle!
		return true;
	}
}

Version data entries

146 entries across 145 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.179 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.178 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.177 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.176 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.175 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.174 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.173 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.172 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.171 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.170 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.169 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.167 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.166 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.165 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.164 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.163 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.162 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.161 tracks/cfml/exercises/triangle/Solution.cfc
trackler-2.2.1.160 tracks/cfml/exercises/triangle/Solution.cfc