Sha256: 0b63aa5bd7f7a519d3d8a0e4632729356f61b7b1fd0f44972d363ea0414da7e0

Contents?: true

Size: 1.51 KB

Versions: 63

Compression:

Stored size: 1.51 KB

Contents

'use strict';

module.exports = function Matrix(matrix) {
  this.rows = matrix.split('\n').map(function (row) {
    return row.split(' ').map(function (val) { return parseInt(val, 10); });
  });

  this.columns = this.rows[0].map(function () {
    return [];
  }).map(function (column, index) {
    return this.rows.map(function (row) { return row[index]; });
  }, this);

  this.indexesOfMaxValues = function (array) {
    var maxValue = array.reduce(function (acc, curr) {
      return Math.max(acc, curr);
    });

    return this.indexsOf(array, maxValue);
  };

  this.indexesOfMinValues = function (array) {
    var minValue = array.reduce(function (acc, curr) {
      return Math.min(acc, curr);
    });

    return this.indexsOf(array, minValue);
  };

  this.indexsOf = function (array, value) {
    return array.map(function (val, index) {
      return val === value ? index : null;
    }).filter(function (val) {
      return val !== null;
    });
  };

  this.calculateSaddlePoints = function (rows, columns) {
    var maxIndexes;
    var minIndexes;
    var saddlePoints = [];

    rows.forEach(function (row, i) {
      maxIndexes = this.indexesOfMaxValues(row);

      maxIndexes.forEach(function (currentMaxIndex) {
        minIndexes = this.indexesOfMinValues(columns[currentMaxIndex]);

        if (minIndexes.indexOf(i) >= 0) {
          saddlePoints.push([i, currentMaxIndex]);
        }
      }, this);
    }, this);

    return saddlePoints;
  };

  this.saddlePoints = this.calculateSaddlePoints(this.rows, this.columns);
};

Version data entries

63 entries across 63 versions & 1 rubygems

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