Repository navigation
Expand file tree
/
Copy pathexample.js
More file actions
83 lines (63 loc) · 1.96 KB
/
Copy pathexample.js
File metadata and controls
83 lines (63 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
function toInt(s) {
return parseInt(s, 10);
}
module.exports = function Matrix(matrix) {
this.rows = [];
this.columns = [];
var rows = matrix.split('\n');
var i, j, currentRow;
for (i = 0; i < rows.length; i++) {
currentRow = rows[i].replace(/^\s+|\s+$/g,'').split(' ').map( toInt );
this.rows.push(currentRow);
}
for (i = 0; i < this.rows[0].length; i++) {
this.columns.push([]);
}
for (i = 0; i < this.rows.length; i++) {
for (j = 0; j < this.columns.length; j++) {
this.columns[j].push(this.rows[i][j]);
}
}
this.indexesOfMaxValues = function(array) {
var i, currentElement, maxValue, indexes = [];
for (i = 0; i < array.length; i++) {
currentElement = array[i];
if (maxValue === undefined || currentElement > maxValue) {
maxValue = currentElement;
indexes = [i];
} else if (currentElement === maxValue) {
indexes.push(i);
}
}
return indexes;
};
this.indexesOfMinValues = function(array) {
var i, currentElement, minValue, indexes = [];
for (i = 0; i < array.length; i++) {
currentElement = array[i];
if (minValue === undefined || currentElement < minValue) {
minValue = currentElement;
indexes = [i];
} else if (currentElement === minValue) {
indexes.push(i);
}
}
return indexes;
};
this.calculateSaddlePoints = function(rows,columns) {
var i, j, maxIndexes, minIndexes, currentMaxIndex, saddlePoints = [];
for (i = 0; i < rows.length; i++) {
maxIndexes = this.indexesOfMaxValues(rows[i]);
for (j = 0; j < maxIndexes.length; j++) {
currentMaxIndex = maxIndexes[j];
minIndexes = this.indexesOfMinValues(columns[currentMaxIndex]);
if (minIndexes.indexOf(i) >= 0) {
saddlePoints.push([i, currentMaxIndex]);
}
}
}
return saddlePoints;
};
this.saddlePoints = this.calculateSaddlePoints(this.rows,this.columns);
};