Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3173c55

Browse files
author
programmiri
committed
Add first solution exercism matrix
1 parent 499f4cc commit 3173c55

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

‎matrix/matrix.js‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
export class Matrix {
22
constructor(string) {
33
this.matrix = string;
4+
this.firstLineNumber = new RegExp(/^([0-9]+)/gm)
5+
this.leadingWhitespace = new RegExp(/^[\t]+/gm)
46
}
57

6-
splitInRows() {
8+
splitInRows(stringMatrix) {
9+
return stringMatrix.split('\n').map( entry => entry.split(' ').map(entry => parseInt(entry, 10)))
10+
}
11+
12+
splitInColumns(stringMatrix) {
13+
const columns = []
714

8-
const rowsFirst = this.matrix.split('\n').map( entry => entry.split(''))
9-
// .map( entry => parseInt(entry))
10-
console.log(rowsFirst)
15+
let matrixToWorkOn = stringMatrix
16+
while (matrixToWorkOn.trim().length) {
17+
const column = matrixToWorkOn.match(this.firstLineNumber).map(entry => parseInt(entry, 10))
18+
columns.push([... column])
19+
matrixToWorkOn = matrixToWorkOn.replace(this.firstLineNumber, '').replace(this.leadingWhitespace, '')
20+
}
21+
return columns
1122
}
1223

1324
get rows() {
14-
const test = this.splitInRows()
15-
// console.log(this.splitInRows())
16-
return test
25+
return this.splitInRows(this.matrix)
1726
}
1827

1928
get columns() {
20-
29+
returnthis.splitInColumns(this.matrix)
2130
}
2231
}

‎matrix/matrix.spec.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
import { Matrix } from './matrix';
22

33
describe('Matrix', () => {
4-
test('extract row from one number matrix', () => {
4+
test('extract row from one number matrix', () => {
55
expect(new Matrix('1').rows[0]).toEqual([1]);
66
});
77

8-
xtest('can extract row', () => {
8+
test('can extract row', () => {
99
expect(new Matrix('1 2\n3 4').rows[1]).toEqual([3, 4]);
1010
});
1111

12-
xtest('extract row where numbers have different widths', () => {
12+
test('extract row where numbers have different widths', () => {
1313
expect(new Matrix('1 2\n10 20').rows[1]).toEqual([10, 20]);
1414
});
1515

16-
xtest('can extract row from non-square matrix with no corresponding column', () => {
16+
test('can extract row from non-square matrix with no corresponding column', () => {
1717
expect(new Matrix('1 2 3\n4 5 6\n7 8 9\n8 7 6').rows[3]).toEqual([8, 7, 6]);
1818
});
1919

20-
xtest('extract column from one number matrix', () => {
20+
test('extract column from one number matrix', () => {
2121
expect(new Matrix('1').columns[0]).toEqual([1]);
2222
});
2323

24-
xtest('can extract column', () => {
24+
test('can extract column', () => {
2525
expect(new Matrix('1 2 3\n4 5 6\n7 8 9').columns[2]).toEqual([3, 6, 9]);
2626
});
2727

28-
xtest('can extract column from non-square matrix with no corresponding row', () => {
28+
test('can extract column from non-square matrix with no corresponding row', () => {
2929
expect(new Matrix('1 2 3 4\n5 6 7 8\n9 8 7 6').columns[3]).toEqual([4, 8, 6]);
3030
});
3131

32-
xtest('can extract column from non-square matrix with more columns than rows', () => {
32+
test('can extract column from non-square matrix with more columns than rows', () => {
3333
expect(new Matrix('1 2 3\n4 5 6').columns[2]).toEqual([3, 6]);
3434
});
3535

36-
xtest('extract column where numbers have different widths', () => {
36+
test('extract column where numbers have different widths', () => {
3737
expect(new Matrix('89 1903 3\n18 3 1\n9 4 800').columns[1]).toEqual([1903, 3, 4]);
3838
});
3939
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /