Skip to content

Commit 66cbde2

Browse files
authored
Add matrix (#336)
1 parent 7eb1680 commit 66cbde2

File tree

7 files changed

+153
-0
lines changed

7 files changed

+153
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@
279279
"pattern_recognition"
280280
]
281281
},
282+
{
283+
"slug": "matrix",
284+
"name": "Matrix",
285+
"uuid": "653ae967-32d2-4789-bb43-289ad1572191",
286+
"practices": [],
287+
"prerequisites": [],
288+
"difficulty": 2
289+
},
282290
{
283291
"slug": "matching-brackets",
284292
"name": "Matching Brackets",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Instructions
2+
3+
Given a string representing a matrix of numbers, return the rows and columns of that matrix.
4+
5+
So given a string with embedded newlines like:
6+
7+
```text
8+
9 8 7
9+
5 3 2
10+
6 6 7
11+
```
12+
13+
representing this matrix:
14+
15+
```text
16+
1 2 3
17+
|---------
18+
1 | 9 8 7
19+
2 | 5 3 2
20+
3 | 6 6 7
21+
```
22+
23+
your code should be able to spit out:
24+
25+
- A list of the rows, reading each row left-to-right while moving top-to-bottom across the rows,
26+
- A list of the columns, reading each column top-to-bottom while moving from left-to-right.
27+
28+
The rows for our example matrix:
29+
30+
- 9, 8, 7
31+
- 5, 3, 2
32+
- 6, 6, 7
33+
34+
And its columns:
35+
36+
- 9, 5, 6
37+
- 8, 3, 6
38+
- 7, 2, 7
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"matrix.coffee"
8+
],
9+
"test": [
10+
"matrix.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Given a string representing a matrix of numbers, return the rows and columns of that matrix.",
17+
"source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.",
18+
"source_url": "https://turing.edu"
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Matrix
2+
constructor: (description) ->
3+
@rows = description.split("\n").map (row) ->
4+
row.split(" ").map (number) -> +number
5+
6+
row: (index) ->
7+
@rows[index - 1]
8+
9+
column: (index) ->
10+
@rows.map (row) -> row[index - 1]
11+
12+
module.exports = Matrix
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[ca733dab-9d85-4065-9ef6-a880a951dafd]
13+
description = "extract row from one number matrix"
14+
15+
[5c93ec93-80e1-4268-9fc2-63bc7d23385c]
16+
description = "can extract row"
17+
18+
[2f1aad89-ad0f-4bd2-9919-99a8bff0305a]
19+
description = "extract row where numbers have different widths"
20+
21+
[68f7f6ba-57e2-4e87-82d0-ad09889b5204]
22+
description = "can extract row from non-square matrix with no corresponding column"
23+
24+
[e8c74391-c93b-4aed-8bfe-f3c9beb89ebb]
25+
description = "extract column from one number matrix"
26+
27+
[7136bdbd-b3dc-48c4-a10c-8230976d3727]
28+
description = "can extract column"
29+
30+
[ad64f8d7-bba6-4182-8adf-0c14de3d0eca]
31+
description = "can extract column from non-square matrix with no corresponding row"
32+
33+
[9eddfa5c-8474-440e-ae0a-f018c2a0dd89]
34+
description = "extract column where numbers have different widths"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Matrix
2+
constructor: (description) ->
3+
4+
row: (index) ->
5+
6+
column: (index) ->
7+
8+
module.exports = Matrix
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Matrix = require './matrix'
2+
3+
describe 'Matrix', ->
4+
it 'extract row from one number matrix', ->
5+
matrix = new Matrix "1"
6+
expect(matrix.row 1).toEqual [1]
7+
8+
xit 'can extract row', ->
9+
matrix = new Matrix "1 2\n3 4"
10+
expect(matrix.row 2).toEqual [3, 4]
11+
12+
xit 'extract row where numbers have different widths', ->
13+
matrix = new Matrix "1 2\n10 20"
14+
expect(matrix.row 2).toEqual [10, 20]
15+
16+
xit 'can extract row from non-square matrix with no corresponding column', ->
17+
matrix = new Matrix "1 2 3\n4 5 6\n7 8 9\n8 7 6"
18+
expect(matrix.row 4).toEqual [8, 7, 6]
19+
20+
xit 'extract column from one number matrix', ->
21+
matrix = new Matrix "1"
22+
expect(matrix.column 1).toEqual [1]
23+
24+
xit 'can extract column', ->
25+
matrix = new Matrix "1 2 3\n4 5 6\n7 8 9"
26+
expect(matrix.column 3).toEqual [3, 6, 9]
27+
28+
xit 'can extract column from non-square matrix with no corresponding row', ->
29+
matrix = new Matrix "1 2 3 4\n5 6 7 8\n9 8 7 6"
30+
expect(matrix.column 4).toEqual [4, 8, 6]
31+
32+
xit 'extract column where numbers have different widths', ->
33+
matrix = new Matrix "89 1903 3\n18 3 1\n9 4 800"
34+
expect(matrix.column 2).toEqual [1903, 3, 4]

0 commit comments

Comments
 (0)