Sha256: ad64033f7d16e17245db52ec518f94a85f914120590714a39f2cb90b2056c638

Contents?: true

Size: 1.17 KB

Versions: 74

Compression:

Stored size: 1.17 KB

Contents

/**
* Here is an example solution for the SaddlePoints exercise
*/
component {

	function saddlePoints( matrix ) {
		if( matrix.isEmpty() || matrix[ 1 ].isEmpty() ) {
			return [];
		}
		
		var result = [];
		// Loop over each column
		for( var col=1; col<=3; col++ ) {
			
			// Loop over each row
			for( var row=1; row<=3; row++ ) {
				
				// This value must be the biggest int he row
				if( getAt( matrix, row, col ) >= rowMax( matrix, row )
					// and the least in the column
					&& getAt( matrix, row, col ) <= colMin( matrix, col ) ){
					
					// Silly exercise uses inferior 0-based numbering!
					result.append( {
						row : row-1,
						column : col-1
					} );
					
				} // End check
				
			} // end row loop
			
		} // end col loop
		
		return result;
	}
	
	/**
	* Get minimum value for a column
	*/
	private function colMin( matrix, col ) {
		return [ matrix[ 1 ][ col ], matrix[ 2 ][ col ], matrix[ 3 ][ col ] ].min();
	}	
	
	/**
	* Get maximum value for a row
	*/
	private function rowMax( matrix, row ) {
		return matrix[ row ].max();
	}	
	
	/**
	* Get a specific value from the matrix
	*/
	private function getAt( matrix, row, col ) {
		return matrix[ row ][ col ];
	}
		
}

Version data entries

74 entries across 74 versions & 1 rubygems

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