Skip to content

Commit 1cf52be

Browse files
feat: add matrix exercise (#179)
* feat: add matrix exercise * use split_into_lines
1 parent d0ff1b9 commit 1cf52be

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,14 @@
601601
"prerequisites": [],
602602
"difficulty": 4
603603
},
604+
{
605+
"slug": "matrix",
606+
"name": "Matrix",
607+
"uuid": "b389b48c-fa60-41f6-8e5e-491b4ca7a346",
608+
"practices": [],
609+
"prerequisites": [],
610+
"difficulty": 4
611+
},
604612
{
605613
"slug": "gigasecond",
606614
"name": "Gigasecond",
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"matrix.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
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+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module main
2+
3+
fn row(str string, index int) []int {
4+
line := str.split_into_lines()[index - 1]
5+
return line.split(' ').map(fn (s string) int {
6+
return s.int()
7+
})
8+
}
9+
10+
fn column(str string, index int) []int {
11+
lines := str.split_into_lines()
12+
return lines.map(fn [index] (line string) int {
13+
return line.split(' ')[index - 1].int()
14+
})
15+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[ca733dab-9d85-4065-9ef6-a880a951dafd]
6+
description = "extract row from one number matrix"
7+
8+
[5c93ec93-80e1-4268-9fc2-63bc7d23385c]
9+
description = "can extract row"
10+
11+
[2f1aad89-ad0f-4bd2-9919-99a8bff0305a]
12+
description = "extract row where numbers have different widths"
13+
14+
[68f7f6ba-57e2-4e87-82d0-ad09889b5204]
15+
description = "can extract row from non-square matrix with no corresponding column"
16+
17+
[e8c74391-c93b-4aed-8bfe-f3c9beb89ebb]
18+
description = "extract column from one number matrix"
19+
20+
[7136bdbd-b3dc-48c4-a10c-8230976d3727]
21+
description = "can extract column"
22+
23+
[ad64f8d7-bba6-4182-8adf-0c14de3d0eca]
24+
description = "can extract column from non-square matrix with no corresponding row"
25+
26+
[9eddfa5c-8474-440e-ae0a-f018c2a0dd89]
27+
description = "extract column where numbers have different widths"

exercises/practice/matrix/matrix.v

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module main
2+
3+
fn row(str string, index int) []int {
4+
}
5+
6+
fn column(str string, index int) []int {
7+
}

exercises/practice/matrix/run_test.v

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module main
2+
3+
fn test_extract_row_from_one_number_matrix() {
4+
str := '1'
5+
assert row(str, 1) == [1]
6+
}
7+
8+
fn test_can_extract_row() {
9+
str := '1 2\n3 4'
10+
assert row(str, 2) == [3, 4]
11+
}
12+
13+
fn test_extract_row_where_numbers_have_different_widths() {
14+
str := '1 2\n10 20'
15+
assert row(str, 2) == [10, 20]
16+
}
17+
18+
fn test_can_extract_row_from_non_square_matrix_with_no_corresponding_column() {
19+
str := '1 2 3\n4 5 6\n7 8 9\n8 7 6'
20+
assert row(str, 4) == [8, 7, 6]
21+
}
22+
23+
fn test_extract_column_from_one_number_matrix() {
24+
str := '1'
25+
assert column(str, 1) == [1]
26+
}
27+
28+
fn test_can_extract_column() {
29+
str := '1 2 3\n4 5 6\n7 8 9'
30+
assert column(str, 3) == [3, 6, 9]
31+
}
32+
33+
fn test_can_extract_column_from_non_square_matrix_with_no_corresponding_row() {
34+
str := '1 2 3 4\n5 6 7 8\n9 8 7 6'
35+
assert column(str, 4) == [4, 8, 6]
36+
}
37+
38+
fn test_extract_column_where_numbers_have_different_widths() {
39+
str := '89 1903 3\n18 3 1\n9 4 800'
40+
assert column(str, 2) == [1903, 3, 4]
41+
}

0 commit comments

Comments
 (0)