Skip to content

Commit f4e4b2d

Browse files
authored
Merge pull request #43 from jimmytty/matrix
* add practice exercise: matrix
2 parents 1281dd3 + c9d3aa2 commit f4e4b2d

File tree

11 files changed

+1038
-0
lines changed

11 files changed

+1038
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@
236236
"prerequisites": [],
237237
"difficulty": 3
238238
},
239+
{
240+
"slug": "matrix",
241+
"name": "Matrix",
242+
"uuid": "dce69631-d5a0-4030-a7b3-3d108efdc39d",
243+
"practices": [],
244+
"prerequisites": [],
245+
"difficulty": 3
246+
},
239247
{
240248
"slug": "line-up",
241249
"name": "Line Up",
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+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"Matrix.pas"
8+
],
9+
"test": [
10+
"TestCases.pas"
11+
],
12+
"example": [
13+
".meta/example.pas"
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
unit Matrix;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TIntArray = Array Of Integer;
9+
TIntMatrix = Array Of TIntArray;
10+
11+
function row(const AString : string; const index : byte) : TIntArray;
12+
function column(const AString : string; const index : byte) : TIntArray;
13+
14+
implementation
15+
16+
uses SysUtils, StrUtils;
17+
18+
function StringToMatrix(const AString : string) : TIntMatrix;
19+
var
20+
LRow : string;
21+
LCol : string;
22+
i : byte = 0;
23+
j : byte = 0;
24+
begin
25+
result := [[]];
26+
for LRow in SplitString(AString, '\n') do
27+
begin
28+
inc(i);
29+
SetLength(result, i);
30+
for LCol in SplitString(LRow, ' ') do
31+
begin
32+
insert(StrToInt(LCol), result[i - 1], j);
33+
inc(j);
34+
end;
35+
end;
36+
end;
37+
38+
function row(const AString : string; const index : byte) : TIntArray;
39+
var
40+
LMatrix : TIntMatrix;
41+
i : byte;
42+
begin
43+
LMatrix := StringToMatrix(AString);
44+
result := [];
45+
for i := low(LMatrix[index - 1]) to high(LMatrix[index - 1]) do
46+
insert(LMatrix[index - 1][i], result, i);
47+
end;
48+
49+
function column(const AString : string; const index : byte) : TIntArray;
50+
var
51+
LMatrix : TIntMatrix;
52+
i : byte;
53+
begin
54+
LMatrix := StringToMatrix(AString);
55+
result := [];
56+
for i := low(LMatrix) to high(LMatrix) do
57+
insert(LMatrix[i][index - 1], result, i);
58+
end;
59+
60+
end.
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/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SHELL = /bin/bash
2+
MAKEFLAGS += --no-print-directory
3+
DESTDIR = build
4+
EXECUTABLE = $(DESTDIR)/test
5+
COMMAND = fpc -l- -v0 -g -gl -Sa -Cr -Sehnw -Fu./lib test.pas -FE"./$(DESTDIR)"
6+
7+
.ONESHELL:
8+
9+
test:
10+
@mkdir -p "./$(DESTDIR)"
11+
@cp -r ./lib "./$(DESTDIR)"
12+
@$(COMMAND) && ./$(EXECUTABLE) $(test)
13+
14+
clean:
15+
@rm -fr "./$(DESTDIR)"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
unit Matrix;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TIntArray = Array Of Integer;
9+
10+
function row(const AString : string; const index : byte) : TIntArray;
11+
function column(const AString : string; const index : byte) : TIntArray;
12+
13+
implementation
14+
15+
uses SysUtils;
16+
17+
function row(const AString : string; const index : byte) : TIntArray;
18+
begin
19+
20+
raise ENotImplemented.Create('Please implement your solution.'); result := [length(AString) * -1, index * -1];
21+
22+
end;
23+
24+
function column(const AString : string; const index : byte) : TIntArray;
25+
begin
26+
27+
raise ENotImplemented.Create('Please implement your solution.'); result := [length(AString) * -1, index * -1];
28+
29+
end;
30+
31+
end.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
unit TestCases;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses Classes, SysUtils, FPCUnit, TestRegistry, FPCUnitTestUtils;
8+
9+
type
10+
MatrixTest = class(TTestCase)
11+
published
12+
procedure extract_row_from_one_number_matrix;
13+
procedure can_extract_row;
14+
procedure extract_row_where_numbers_have_different_widths;
15+
procedure can_extract_row_from_non_square_matrix_with_no_corresponding_column;
16+
procedure extract_column_from_one_number_matrix;
17+
procedure can_extract_column;
18+
procedure can_extract_column_from_non_square_matrix_with_no_corresponding_row;
19+
procedure extract_column_where_numbers_have_different_widths;
20+
end;
21+
22+
implementation
23+
24+
uses Matrix;
25+
26+
// ca733dab-9d85-4065-9ef6-a880a951dafd
27+
procedure MatrixTest.extract_row_from_one_number_matrix;
28+
begin
29+
TapAssertTrue(Self, 'extract row from one number matrix', [1], Matrix.row('1', 1));
30+
end;
31+
32+
// 5c93ec93-80e1-4268-9fc2-63bc7d23385c
33+
procedure MatrixTest.can_extract_row;
34+
begin
35+
TapAssertTrue(Self, 'can extract row', [3, 4], Matrix.row('1 2\n3 4', 2));
36+
end;
37+
38+
// 2f1aad89-ad0f-4bd2-9919-99a8bff0305a
39+
procedure MatrixTest.extract_row_where_numbers_have_different_widths;
40+
begin
41+
TapAssertTrue(Self, 'extract row where numbers have different widths', [10, 20], Matrix.row('1 2\n10 20', 2));
42+
end;
43+
44+
// 68f7f6ba-57e2-4e87-82d0-ad09889b5204
45+
procedure MatrixTest.can_extract_row_from_non_square_matrix_with_no_corresponding_column;
46+
begin
47+
TapAssertTrue(Self, 'can extract row from non-square matrix with no corresponding column', [8, 7, 6], Matrix.row('1 2 3\n4 5 6\n7 8 9\n8 7 6', 4));
48+
end;
49+
50+
// e8c74391-c93b-4aed-8bfe-f3c9beb89ebb
51+
procedure MatrixTest.extract_column_from_one_number_matrix;
52+
begin
53+
TapAssertTrue(Self, 'extract column from one number matrix', [1], Matrix.column('1', 1));
54+
end;
55+
56+
// 7136bdbd-b3dc-48c4-a10c-8230976d3727
57+
procedure MatrixTest.can_extract_column;
58+
begin
59+
TapAssertTrue(Self, 'can extract column', [3, 6, 9], Matrix.column('1 2 3\n4 5 6\n7 8 9', 3));
60+
end;
61+
62+
// ad64f8d7-bba6-4182-8adf-0c14de3d0eca
63+
procedure MatrixTest.can_extract_column_from_non_square_matrix_with_no_corresponding_row;
64+
begin
65+
TapAssertTrue(Self, 'can extract column from non-square matrix with no corresponding row', [4, 8, 6], Matrix.column('1 2 3 4\n5 6 7 8\n9 8 7 6', 4));
66+
end;
67+
68+
// 9eddfa5c-8474-440e-ae0a-f018c2a0dd89
69+
procedure MatrixTest.extract_column_where_numbers_have_different_widths;
70+
begin
71+
TapAssertTrue(Self, 'extract column where numbers have different widths', [1903, 3, 4], Matrix.column('89 1903 3\n18 3 1\n9 4 800', 2));
72+
end;
73+
74+
initialization
75+
RegisterTest(MatrixTest);
76+
77+
end.

0 commit comments

Comments
 (0)