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.139 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.138 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.137 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.136 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.135 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.134 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.133 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.132 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.131 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.130 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.129 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.128 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.127 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.126 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.125 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.124 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.123 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.122 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.121 tracks/javascript/exercises/saddle-points/example.js
trackler-2.2.1.120 tracks/javascript/exercises/saddle-points/example.js