Skip to content

Commit 768d28f

Browse files
Add matrix (#502)
1 parent b5e825d commit 768d28f

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,14 @@
846846
],
847847
"difficulty": 4
848848
},
849+
{
850+
"slug": "matrix",
851+
"name": "Matrix",
852+
"uuid": "fc517d08-fb7f-49ed-a474-ce78d3c5ac7d",
853+
"practices": [],
854+
"prerequisites": [],
855+
"difficulty": 4
856+
},
849857
{
850858
"slug": "nth-prime",
851859
"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.zig"
8+
],
9+
"test": [
10+
"test_matrix.zig"
11+
],
12+
"example": [
13+
".meta/example.zig"
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const std = @import("std");
2+
const mem = std.mem;
3+
4+
/// Returns the selected row of the matrix.
5+
pub fn row(allocator: mem.Allocator, s: []const u8, index: i32) ![]i16 {
6+
return select(allocator, s, index, 0);
7+
}
8+
9+
/// Returns the selected column of the matrix.
10+
pub fn column(allocator: mem.Allocator, s: []const u8, index: i32) ![]i16 {
11+
return select(allocator, s, 0, index);
12+
}
13+
14+
pub fn select(allocator: mem.Allocator, s: []const u8, required_row: i32, required_column: i32) ![]i16 {
15+
var list = std.array_list.Managed(i16).init(allocator);
16+
errdefer list.deinit();
17+
var current_row: i32 = 1;
18+
var current_column: i32 = 1;
19+
var number: i16 = 0;
20+
var negate: bool = false;
21+
22+
for (0.., s) |i, c| {
23+
switch (c) {
24+
'0'...'9' => {
25+
number = 10 * number + (c - '0');
26+
if (i + 1 == s.len or s[i + 1] < '0') {
27+
if (negate) {
28+
number = -number;
29+
negate = false;
30+
}
31+
if (current_row == required_row or current_column == required_column) {
32+
try list.append(number);
33+
}
34+
number = 0;
35+
}
36+
},
37+
' ' => {
38+
current_column += 1;
39+
},
40+
'\n' => {
41+
current_row += 1;
42+
current_column = 1;
43+
},
44+
'-' => {
45+
negate = true;
46+
},
47+
else => unreachable,
48+
}
49+
}
50+
return list.toOwnedSlice();
51+
}
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const std = @import("std");
2+
const mem = std.mem;
3+
4+
/// Returns the selected row of the matrix.
5+
pub fn row(allocator: mem.Allocator, s: []const u8, index: i32) ![]i16 {
6+
_ = allocator;
7+
_ = s;
8+
_ = index;
9+
@compileError("please implement the row function");
10+
}
11+
12+
/// Returns the selected column of the matrix.
13+
pub fn column(allocator: mem.Allocator, s: []const u8, index: i32) ![]i16 {
14+
_ = allocator;
15+
_ = s;
16+
_ = index;
17+
@compileError("please implement the column function");
18+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const std = @import("std");
2+
const testing = std.testing;
3+
4+
const matrix = @import("matrix.zig");
5+
const row = matrix.row;
6+
const column = matrix.column;
7+
8+
test "extract row from one number matrix" {
9+
const expected = &[_]i16{1};
10+
const s = "1";
11+
const actual = try row(testing.allocator, s, 1);
12+
defer testing.allocator.free(actual);
13+
try testing.expectEqualSlices(i16, expected, actual);
14+
}
15+
16+
test "can extract row" {
17+
const expected = &[_]i16{ 3, 4 };
18+
const s = "1 2\n3 4";
19+
const actual = try row(testing.allocator, s, 2);
20+
defer testing.allocator.free(actual);
21+
try testing.expectEqualSlices(i16, expected, actual);
22+
}
23+
24+
test "extract row where numbers have different widths" {
25+
const expected = &[_]i16{ 10, 20 };
26+
const s = "1 2\n10 20";
27+
const actual = try row(testing.allocator, s, 2);
28+
defer testing.allocator.free(actual);
29+
try testing.expectEqualSlices(i16, expected, actual);
30+
}
31+
32+
test "can extract row from non-square matrix with no corresponding column" {
33+
const expected = &[_]i16{ 8, 7, 6 };
34+
const s = "1 2 3\n4 5 6\n7 8 9\n8 7 6";
35+
const actual = try row(testing.allocator, s, 4);
36+
defer testing.allocator.free(actual);
37+
try testing.expectEqualSlices(i16, expected, actual);
38+
}
39+
40+
test "extract column from one number matrix" {
41+
const expected = &[_]i16{1};
42+
const s = "1";
43+
const actual = try column(testing.allocator, s, 1);
44+
defer testing.allocator.free(actual);
45+
try testing.expectEqualSlices(i16, expected, actual);
46+
}
47+
48+
test "can extract column" {
49+
const expected = &[_]i16{ 3, 6, 9 };
50+
const s = "1 2 3\n4 5 6\n7 8 9";
51+
const actual = try column(testing.allocator, s, 3);
52+
defer testing.allocator.free(actual);
53+
try testing.expectEqualSlices(i16, expected, actual);
54+
}
55+
56+
test "can extract column from non-square matrix with no corresponding row" {
57+
const expected = &[_]i16{ 4, 8, 6 };
58+
const s = "1 2 3 4\n5 6 7 8\n9 8 7 6";
59+
const actual = try column(testing.allocator, s, 4);
60+
defer testing.allocator.free(actual);
61+
try testing.expectEqualSlices(i16, expected, actual);
62+
}
63+
64+
test "extract column where numbers have different widths" {
65+
const expected = &[_]i16{ 1903, 3, 4 };
66+
const s = "89 1903 3\n18 3 1\n9 4 800";
67+
const actual = try column(testing.allocator, s, 2);
68+
defer testing.allocator.free(actual);
69+
try testing.expectEqualSlices(i16, expected, actual);
70+
}
71+
72+
test "row with negative numbers" {
73+
const expected = &[_]i16{ -57, 9, -42 };
74+
const s = "1 2 4\n-57 9 -42\n10 0 65";
75+
const actual = try row(testing.allocator, s, 2);
76+
defer testing.allocator.free(actual);
77+
try testing.expectEqualSlices(i16, expected, actual);
78+
}
79+
80+
test "column with negative numbers" {
81+
const expected = &[_]i16{ -4, -42, -465 };
82+
const s = "1 2 -4\n-57 9 -42\n10 0 -465";
83+
const actual = try column(testing.allocator, s, 3);
84+
defer testing.allocator.free(actual);
85+
try testing.expectEqualSlices(i16, expected, actual);
86+
}

0 commit comments

Comments
 (0)