Skip to content

Commit

Permalink
Add matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Aug 8, 2024
1 parent c7939d6 commit a91507c
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@
"pattern_recognition"
]
},
{
"slug": "matrix",
"name": "Matrix",
"uuid": "653ae967-32d2-4789-bb43-289ad1572191",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "matching-brackets",
"name": "Matching Brackets",
Expand Down
38 changes: 38 additions & 0 deletions exercises/practice/matrix/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Instructions

Given a string representing a matrix of numbers, return the rows and columns of that matrix.

So given a string with embedded newlines like:

```text
9 8 7
5 3 2
6 6 7
```

representing this matrix:

```text
1 2 3
|---------
1 | 9 8 7
2 | 5 3 2
3 | 6 6 7
```

your code should be able to spit out:

- A list of the rows, reading each row left-to-right while moving top-to-bottom across the rows,
- A list of the columns, reading each column top-to-bottom while moving from left-to-right.

The rows for our example matrix:

- 9, 8, 7
- 5, 3, 2
- 6, 6, 7

And its columns:

- 9, 5, 6
- 8, 3, 6
- 7, 2, 7
19 changes: 19 additions & 0 deletions exercises/practice/matrix/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"matrix.coffee"
],
"test": [
"matrix.spec.coffee"
],
"example": [
".meta/example.coffee"
]
},
"blurb": "Given a string representing a matrix of numbers, return the rows and columns of that matrix.",
"source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.",
"source_url": "https://turing.edu"
}
12 changes: 12 additions & 0 deletions exercises/practice/matrix/.meta/example.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Matrix
constructor: (description) ->
@rows = description.split("\n").map (row) ->
row.split(" ").map (number) -> +number

row: (index) ->
@rows[index - 1]

column: (index) ->
@rows.map (row) -> row[index - 1]

module.exports = Matrix
34 changes: 34 additions & 0 deletions exercises/practice/matrix/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[ca733dab-9d85-4065-9ef6-a880a951dafd]
description = "extract row from one number matrix"

[5c93ec93-80e1-4268-9fc2-63bc7d23385c]
description = "can extract row"

[2f1aad89-ad0f-4bd2-9919-99a8bff0305a]
description = "extract row where numbers have different widths"

[68f7f6ba-57e2-4e87-82d0-ad09889b5204]
description = "can extract row from non-square matrix with no corresponding column"

[e8c74391-c93b-4aed-8bfe-f3c9beb89ebb]
description = "extract column from one number matrix"

[7136bdbd-b3dc-48c4-a10c-8230976d3727]
description = "can extract column"

[ad64f8d7-bba6-4182-8adf-0c14de3d0eca]
description = "can extract column from non-square matrix with no corresponding row"

[9eddfa5c-8474-440e-ae0a-f018c2a0dd89]
description = "extract column where numbers have different widths"
8 changes: 8 additions & 0 deletions exercises/practice/matrix/matrix.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Matrix
constructor: (description) ->

row: (index) ->

column: (index) ->

module.exports = Matrix
34 changes: 34 additions & 0 deletions exercises/practice/matrix/matrix.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Matrix = require './matrix'

describe 'Matrix', ->
it 'extract row from one number matrix', ->
matrix = new Matrix "1"
expect(matrix.row 1).toEqual [1]

xit 'can extract row', ->
matrix = new Matrix "1 2\n3 4"
expect(matrix.row 2).toEqual [3, 4]

xit 'extract row where numbers have different widths', ->
matrix = new Matrix "1 2\n10 20"
expect(matrix.row 2).toEqual [10, 20]

xit 'can extract row from non-square matrix with no corresponding column', ->
matrix = new Matrix "1 2 3\n4 5 6\n7 8 9\n8 7 6"
expect(matrix.row 4).toEqual [8, 7, 6]

xit 'extract column from one number matrix', ->
matrix = new Matrix "1"
expect(matrix.column 1).toEqual [1]

xit 'can extract column', ->
matrix = new Matrix "1 2 3\n4 5 6\n7 8 9"
expect(matrix.column 3).toEqual [3, 6, 9]

xit 'can extract column from non-square matrix with no corresponding row', ->
matrix = new Matrix "1 2 3 4\n5 6 7 8\n9 8 7 6"
expect(matrix.column 4).toEqual [4, 8, 6]

xit 'extract column where numbers have different widths', ->
matrix = new Matrix "89 1903 3\n18 3 1\n9 4 800"
expect(matrix.column 2).toEqual [1903, 3, 4]

0 comments on commit a91507c

Please sign in to comment.