Skip to content

Commit ac1f950

Browse files
Add matrix (#489)
1 parent 9ba6e00 commit ac1f950

File tree

7 files changed

+183
-0
lines changed

7 files changed

+183
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,14 @@
590590
"prerequisites": [],
591591
"difficulty": 4
592592
},
593+
{
594+
"slug": "matrix",
595+
"name": "Matrix",
596+
"uuid": "4f30e2a6-97d9-4082-a1fc-82fa8767ffe9",
597+
"practices": [],
598+
"prerequisites": [],
599+
"difficulty": 4
600+
},
593601
{
594602
"slug": "nth-prime",
595603
"name": "Nth Prime",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"matrix.el"
8+
],
9+
"test": [
10+
"matrix-test.el"
11+
],
12+
"example": [
13+
".meta/example.el"
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
;;; matrix.el --- Matrix (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
(require 'seq)
8+
9+
(defun row (string index)
10+
(seq-into (mapcar #'string-to-number
11+
(split-string (nth (1- index)
12+
(split-string string "\n")) " ")) 'vector))
13+
14+
(defun column (string index)
15+
(let ((extract (lambda (line)
16+
(string-to-number (nth (1- index)
17+
(split-string line " "))))))
18+
(seq-into (seq-map extract
19+
(split-string string "\n")) 'vector)))
20+
21+
(provide 'matrix)
22+
;;; matrix.el ends here
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
;;; matrix-test.el --- Tests for Matrix (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(load-file "matrix.el")
9+
(declare-function row "matrix.el" (string index))
10+
(declare-function column "matrix.el" (string index))
11+
12+
13+
(ert-deftest extract-row-from-one-number-matrix ()
14+
(should (equal [1] (row "1" 1))))
15+
16+
17+
(ert-deftest can-extract-row ()
18+
(should (equal [3 4] (row "1 2\n3 4" 2))))
19+
20+
21+
(ert-deftest extract-row-where-numbers-have-different-widths ()
22+
(should (equal [10 20] (row "1 2\n10 20" 2))))
23+
24+
25+
(ert-deftest can-extract-row-from-non-square-matrix-with-no-corresponding-column ()
26+
(should (equal [8 7 6] (row "1 2 3\n4 5 6\n7 8 9\n8 7 6" 4))))
27+
28+
29+
(ert-deftest extract-column-from-one-number-matrix ()
30+
(should (equal [1] (column "1" 1))))
31+
32+
33+
(ert-deftest can-extract-column ()
34+
(should (equal [3 6 9] (column "1 2 3\n4 5 6\n7 8 9" 3))))
35+
36+
37+
(ert-deftest can-extract-column-from-non-square-matrix-with-no-corresponding-row ()
38+
(should (equal [4 8 6] (column "1 2 3 4\n5 6 7 8\n9 8 7 6" 4))))
39+
40+
41+
(ert-deftest extract-column-where-numbers-have-different-widths ()
42+
(should (equal [1903 3 4] (column "89 1903 3\n18 3 1\n9 4 800" 2))))
43+
44+
45+
(provide 'matrix-test)
46+
;;; matrix-test.el ends here
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
;;; matrix.el --- Matrix (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defun row (string index)
9+
(error "Delete this S-Expression and write your own implementation"))
10+
11+
(defun column (string index)
12+
(error "Delete this S-Expression and write your own implementation"))
13+
14+
15+
(provide 'matrix)
16+
;;; matrix.el ends here

0 commit comments

Comments
 (0)