Skip to content

Commit 68f3554

Browse files
committed
Add matrix
1 parent 1418a60 commit 68f3554

6 files changed

Lines changed: 7211 additions & 0 deletions

File tree

matrix/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# metadata
2+
.exercism
3+
4+
# Dependency directories
5+
node_modules

matrix/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Matrix
2+
3+
Given a string representing a matrix of numbers, return the rows and columns of
4+
that matrix.
5+
6+
So given a string with embedded newlines like:
7+
8+
```text
9+
9 8 7
10+
5 3 2
11+
6 6 7
12+
```
13+
14+
representing this matrix:
15+
16+
```text
17+
0 1 2
18+
|---------
19+
0 | 9 8 7
20+
1 | 5 3 2
21+
2 | 6 6 7
22+
```
23+
24+
your code should be able to spit out:
25+
26+
- A list of the rows, reading each row left-to-right while moving
27+
top-to-bottom across the rows,
28+
- A list of the columns, reading each column top-to-bottom while moving
29+
from left-to-right.
30+
31+
The rows for our example matrix:
32+
33+
- 9, 8, 7
34+
- 5, 3, 2
35+
- 6, 6, 7
36+
37+
And its columns:
38+
39+
- 9, 5, 6
40+
- 8, 3, 6
41+
- 7, 2, 7
42+
43+
## Setup
44+
45+
Go through the setup instructions for Javascript to
46+
install the necessary dependencies:
47+
48+
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
49+
50+
## Requirements
51+
52+
Install assignment dependencies:
53+
54+
```bash
55+
$ npm install
56+
```
57+
58+
## Making the test suite pass
59+
60+
Execute the tests with:
61+
62+
```bash
63+
$ npm test
64+
```
65+
66+
In the test suites all tests but the first have been skipped.
67+
68+
Once you get a test passing, you can enable the next one by
69+
changing `xtest` to `test`.
70+
71+
72+
## Source
73+
74+
Warmup to the `saddle-points` warmup. [http://jumpstartlab.com](http://jumpstartlab.com)
75+
76+
## Submitting Incomplete Solutions
77+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

matrix/matrix.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export class Matrix {
2+
constructor(str){
3+
this.str = str;
4+
this.rows = this.getRows();
5+
this.columns = this.getColumns();
6+
}
7+
8+
getRows(){
9+
return this.str.split('\n')
10+
.map(element => element
11+
.split(' ')
12+
.map(element => parseInt(element))
13+
);
14+
}
15+
getColumns() {
16+
return this.rows
17+
.map((item, index, array) => array
18+
.map(element => element[index])
19+
);
20+
}
21+
}

matrix/matrix.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Matrix } from './matrix';
2+
3+
describe('Matrix', () => {
4+
test('extract row from one number matrix', () => {
5+
expect(new Matrix('1').rows[0]).toEqual([1]);
6+
});
7+
8+
test('can extract row', () => {
9+
expect(new Matrix('1 2\n3 4').rows[1]).toEqual([3, 4]);
10+
});
11+
12+
test('extract row where numbers have different widths', () => {
13+
expect(new Matrix('1 2\n10 20').rows[1]).toEqual([10, 20]);
14+
});
15+
16+
test('can extract row from non-square matrix', () => {
17+
expect(new Matrix('1 2 3\n4 5 6\n7 8 9\n8 7 6').rows[2]).toEqual([7, 8, 9]);
18+
});
19+
20+
test('extract column from one number matrix', () => {
21+
expect(new Matrix('1').columns[0]).toEqual([1]);
22+
});
23+
24+
test('can extract column', () => {
25+
expect(new Matrix('1 2 3\n4 5 6\n7 8 9').columns[2]).toEqual([3, 6, 9]);
26+
});
27+
28+
test('can extract column from non-square matrix', () => {
29+
expect(new Matrix('1 2 3\n4 5 6\n7 8 9\n8 7 6').columns[2]).toEqual([3, 6, 9, 6]);
30+
});
31+
32+
test('extract column where numbers have different widths', () => {
33+
expect(new Matrix('89 1903 3\n18 3 1\n9 4 800').columns[1]).toEqual([1903, 3, 4]);
34+
});
35+
});

0 commit comments

Comments
 (0)