Skip to content

Commit dd71f8a

Browse files
authored
Add matrix exercise (#761)
1 parent df41143 commit dd71f8a

File tree

10 files changed

+190
-0
lines changed

10 files changed

+190
-0
lines changed

config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,17 @@
11411141
"math"
11421142
]
11431143
},
1144+
{
1145+
"slug": "matrix",
1146+
"name": "Matrix",
1147+
"uuid": "24ff1abf-c242-4054-ada4-19d6fd4f8c3c",
1148+
"practices": [],
1149+
"prerequisites": [
1150+
"strings",
1151+
"vectors"
1152+
],
1153+
"difficulty": 4
1154+
},
11441155
{
11451156
"slug": "meetup",
11461157
"name": "Meetup",
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+
"tasxatzial"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/matrix.clj"
8+
],
9+
"test": [
10+
"test/matrix_test.clj"
11+
],
12+
"example": [
13+
".meta/example.clj"
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(ns matrix
2+
(:require [clojure.string :as str]))
3+
4+
(defn get-row
5+
"Returns the i-th row of the matrix s"
6+
[s i]
7+
(let [lines (str/split-lines s)
8+
row (str/split (get lines (dec i)) #" ")]
9+
(map #(Integer/parseInt %) row)))
10+
11+
(defn get-column
12+
"Returns the i-th column of the matrix s"
13+
[s i]
14+
(let [lines (str/split-lines s)
15+
matrix (map #(str/split % #" ") lines)
16+
column (map #(get % (dec i)) matrix)]
17+
(map #(Integer/parseInt %) column)))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(ns matrix-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
matrix))
4+
{{#test_cases.row}}
5+
(deftest get-row_test_{{idx}}
6+
(testing {{string description}}
7+
(is (= {{expected}} (matrix/get-row {{string input.string}} {{input.index}})))))
8+
{{/test_cases.row~}}
9+
{{#test_cases.column}}
10+
(deftest get-column_test_{{idx}}
11+
(testing {{string description}}
12+
(is (= {{expected}} (matrix/get-column {{string input.string}} {{input.index}})))))
13+
{{/test_cases.column~}}
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"

exercises/practice/matrix/deps.edn

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{:aliases {:test {:extra-paths ["test"]
2+
:extra-deps {io.github.cognitect-labs/test-runner
3+
{:git/url "https://github.com/cognitect-labs/test-runner.git"
4+
:sha "705ad25bbf0228b1c38d0244a36001c2987d7337"}}
5+
:main-opts ["-m" "cognitect.test-runner"]
6+
:exec-fn cognitect.test-runner.api/test}}}

exercises/practice/matrix/project.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(defproject matrix "0.1.0-SNAPSHOT"
2+
:description "matrix exercise."
3+
:url "https://github.com/exercism/clojure/tree/main/exercises/practice/matrix"
4+
:dependencies [[org.clojure/clojure "1.11.1"]])
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(ns matrix)
2+
3+
(defn get-row
4+
"Returns the i-th row of the matrix s"
5+
[s i]
6+
;; function body
7+
)
8+
9+
(defn get-column
10+
"Returns the i-th column of the matrix s"
11+
[s i]
12+
;; function body
13+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(ns matrix-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
matrix))
4+
5+
(deftest get-row_test_1
6+
(testing "extract row from one number matrix"
7+
(is (= [1] (matrix/get-row "1" 1)))))
8+
9+
(deftest get-row_test_2
10+
(testing "can extract row"
11+
(is (= [3 4] (matrix/get-row "1 2\n3 4" 2)))))
12+
13+
(deftest get-row_test_3
14+
(testing "extract row where numbers have different widths"
15+
(is (= [10 20] (matrix/get-row "1 2\n10 20" 2)))))
16+
17+
(deftest get-row_test_4
18+
(testing "can extract row from non-square matrix with no corresponding column"
19+
(is (= [8 7 6] (matrix/get-row "1 2 3\n4 5 6\n7 8 9\n8 7 6" 4)))))
20+
21+
(deftest get-column_test_1
22+
(testing "extract column from one number matrix"
23+
(is (= [1] (matrix/get-column "1" 1)))))
24+
25+
(deftest get-column_test_2
26+
(testing "can extract column"
27+
(is (= [3 6 9] (matrix/get-column "1 2 3\n4 5 6\n7 8 9" 3)))))
28+
29+
(deftest get-column_test_3
30+
(testing "can extract column from non-square matrix with no corresponding row"
31+
(is (= [4 8 6] (matrix/get-column "1 2 3 4\n5 6 7 8\n9 8 7 6" 4)))))
32+
33+
(deftest get-column_test_4
34+
(testing "extract column where numbers have different widths"
35+
(is (= [1903 3 4] (matrix/get-column "89 1903 3\n18 3 1\n9 4 800" 2)))))

0 commit comments

Comments
 (0)