Skip to content

Commit d3ed7a6

Browse files
Add transpose exercise (#103)
1 parent c19ca34 commit d3ed7a6

File tree

11 files changed

+1103
-0
lines changed

11 files changed

+1103
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,14 @@
700700
"prerequisites": [],
701701
"difficulty": 5
702702
},
703+
{
704+
"slug": "transpose",
705+
"name": "Transpose",
706+
"uuid": "24dd04bb-ebf4-4241-9b17-f0eb8bb54f36",
707+
"practices": [],
708+
"prerequisites": [],
709+
"difficulty": 5
710+
},
703711
{
704712
"slug": "variable-length-quantity",
705713
"name": "Variable Length Quantity",
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Instructions
2+
3+
Given an input text output it transposed.
4+
5+
Roughly explained, the transpose of a matrix:
6+
7+
```text
8+
ABC
9+
DEF
10+
```
11+
12+
is given by:
13+
14+
```text
15+
AD
16+
BE
17+
CF
18+
```
19+
20+
Rows become columns and columns become rows.
21+
See [transpose][].
22+
23+
If the input has rows of different lengths, this is to be solved as follows:
24+
25+
- Pad to the left with spaces.
26+
- Don't pad to the right.
27+
28+
Therefore, transposing this matrix:
29+
30+
```text
31+
ABC
32+
DE
33+
```
34+
35+
results in:
36+
37+
```text
38+
AD
39+
BE
40+
C
41+
```
42+
43+
And transposing:
44+
45+
```text
46+
AB
47+
DEF
48+
```
49+
50+
results in:
51+
52+
```text
53+
AD
54+
BE
55+
F
56+
```
57+
58+
In general, all characters from the input should also be present in the transposed output.
59+
That means that if a column in the input text contains only spaces on its bottom-most row(s), the corresponding output row should contain the spaces in its right-most column(s).
60+
61+
[transpose]: https://en.wikipedia.org/wiki/Transpose
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+
"Transpose.pas"
8+
],
9+
"test": [
10+
"TestCases.pas"
11+
],
12+
"example": [
13+
".meta/example.pas"
14+
]
15+
},
16+
"blurb": "Take input text and output it transposed.",
17+
"source": "Reddit r/dailyprogrammer challenge #270 [Easy].",
18+
"source_url": "https://web.archive.org/web/20230630051421/https://old.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text/"
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
unit Transpose;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TStrArray = array of string;
9+
10+
function transpose(const lines : TStrArray) : TStrArray;
11+
12+
implementation
13+
14+
function transpose(const lines : TStrArray) : TStrArray;
15+
var
16+
r, c : integer;
17+
col : string;
18+
begin
19+
result := nil;
20+
c := 1;
21+
repeat
22+
col := '';
23+
for r := High(lines) downto Low(lines) do
24+
if c <= length(lines[r]) then
25+
col := lines[r][c] + col
26+
else if col <> '' then
27+
col := ' ' + col;
28+
inc(c);
29+
if col <> '' then
30+
insert(col, result, length(result));
31+
until col = '';
32+
end;
33+
34+
end.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
[404b7262-c050-4df0-a2a2-0cb06cd6a821]
13+
description = "empty string"
14+
15+
[a89ce8a3-c940-4703-a688-3ea39412fbcb]
16+
description = "two characters in a row"
17+
18+
[855bb6ae-4180-457c-abd0-ce489803ce98]
19+
description = "two characters in a column"
20+
21+
[5ceda1c0-f940-441c-a244-0ced197769c8]
22+
description = "simple"
23+
24+
[a54675dd-ae7d-4a58-a9c4-0c20e99a7c1f]
25+
description = "single line"
26+
27+
[0dc2ec0b-549d-4047-aeeb-8029fec8d5c5]
28+
description = "first line longer than second line"
29+
30+
[984e2ec3-b3d3-4b53-8bd6-96f5ef404102]
31+
description = "second line longer than first line"
32+
33+
[eccd3784-45f0-4a3f-865a-360cb323d314]
34+
description = "mixed line length"
35+
36+
[85b96b3f-d00c-4f80-8ca2-c8a5c9216c2d]
37+
description = "square"
38+
39+
[b9257625-7a53-4748-8863-e08e9d27071d]
40+
description = "rectangle"
41+
42+
[b80badc9-057e-4543-bd07-ce1296a1ea2c]
43+
description = "triangle"
44+
45+
[76acfd50-5596-4d05-89f1-5116328a7dd9]
46+
description = "jagged triangle"
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: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
unit TestCases;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses Classes, SysUtils, FPCUnit, TestRegistry, FPCUnitTestUtils;
8+
9+
type
10+
TransposeTest = class(TTestCase)
11+
published
12+
procedure empty_string;
13+
procedure two_characters_in_a_row;
14+
procedure two_characters_in_a_column;
15+
procedure simple;
16+
procedure single_line;
17+
procedure first_line_longer_than_second_line;
18+
procedure second_line_longer_than_first_line;
19+
procedure mixed_line_length;
20+
procedure square;
21+
procedure rectangle;
22+
procedure triangle;
23+
procedure jagged_triangle;
24+
end;
25+
26+
implementation
27+
28+
uses Transpose;
29+
30+
// 404b7262-c050-4df0-a2a2-0cb06cd6a821
31+
procedure TransposeTest.empty_string;
32+
const
33+
lines : TStrArray = ();
34+
expect : TStrArray = ();
35+
begin
36+
TapAssertTrue(Self, 'empty string', expect, Transpose.transpose(lines));
37+
end;
38+
39+
// a89ce8a3-c940-4703-a688-3ea39412fbcb
40+
procedure TransposeTest.two_characters_in_a_row;
41+
const
42+
lines : TStrArray = ('A1');
43+
expect : TStrArray = ('A', '1');
44+
begin
45+
TapAssertTrue(Self, 'two characters in a row', expect, Transpose.transpose(lines));
46+
end;
47+
48+
// 855bb6ae-4180-457c-abd0-ce489803ce98
49+
procedure TransposeTest.two_characters_in_a_column;
50+
const
51+
lines : TStrArray = ('A', '1');
52+
expect : TStrArray = ('A1');
53+
begin
54+
TapAssertTrue(Self, 'two characters in a column', expect, Transpose.transpose(lines));
55+
end;
56+
57+
// 5ceda1c0-f940-441c-a244-0ced197769c8
58+
procedure TransposeTest.simple;
59+
const
60+
lines : TStrArray = ('ABC', '123');
61+
expect : TStrArray = ('A1', 'B2', 'C3');
62+
begin
63+
TapAssertTrue(Self, 'simple', expect, Transpose.transpose(lines));
64+
end;
65+
66+
// a54675dd-ae7d-4a58-a9c4-0c20e99a7c1f
67+
procedure TransposeTest.single_line;
68+
const
69+
lines : TStrArray = ('Single line.');
70+
expect : TStrArray = ('S', 'i', 'n', 'g', 'l', 'e', ' ', 'l', 'i', 'n', 'e', '.');
71+
begin
72+
TapAssertTrue(Self, 'single line', expect, Transpose.transpose(lines));
73+
end;
74+
75+
// 0dc2ec0b-549d-4047-aeeb-8029fec8d5c5
76+
procedure TransposeTest.first_line_longer_than_second_line;
77+
const
78+
lines : TStrArray = ('The fourth line.', 'The fifth line.');
79+
expect : TStrArray = ('TT', 'hh', 'ee', ' ', 'ff', 'oi', 'uf', 'rt', 'th', 'h ', ' l', 'li', 'in', 'ne', 'e.', '.');
80+
begin
81+
TapAssertTrue(Self, 'first line longer than second line', expect, Transpose.transpose(lines));
82+
end;
83+
84+
// 984e2ec3-b3d3-4b53-8bd6-96f5ef404102
85+
procedure TransposeTest.second_line_longer_than_first_line;
86+
const
87+
lines : TStrArray = ('The first line.', 'The second line.');
88+
expect : TStrArray = ('TT', 'hh', 'ee', ' ', 'fs', 'ie', 'rc', 'so', 'tn', ' d', 'l ', 'il', 'ni', 'en', '.e', ' .');
89+
begin
90+
TapAssertTrue(Self, 'second line longer than first line', expect, Transpose.transpose(lines));
91+
end;
92+
93+
// eccd3784-45f0-4a3f-865a-360cb323d314
94+
procedure TransposeTest.mixed_line_length;
95+
const
96+
lines : TStrArray = ('The longest line.', 'A long line.', 'A longer line.', 'A line.');
97+
expect : TStrArray = ('TAAA', 'h ', 'elll', ' ooi', 'lnnn', 'ogge', 'n e.', 'glr', 'ei ', 'snl', 'tei', ' .n', 'l e', 'i .', 'n', 'e', '.');
98+
begin
99+
TapAssertTrue(Self, 'mixed line length', expect, Transpose.transpose(lines));
100+
end;
101+
102+
// 85b96b3f-d00c-4f80-8ca2-c8a5c9216c2d
103+
procedure TransposeTest.square;
104+
const
105+
lines : TStrArray = ('HEART', 'EMBER', 'ABUSE', 'RESIN', 'TREND');
106+
expect : TStrArray = ('HEART', 'EMBER', 'ABUSE', 'RESIN', 'TREND');
107+
begin
108+
TapAssertTrue(Self, 'square', expect, Transpose.transpose(lines));
109+
end;
110+
111+
// b9257625-7a53-4748-8863-e08e9d27071d
112+
procedure TransposeTest.rectangle;
113+
const
114+
lines : TStrArray = ('FRACTURE', 'OUTLINED', 'BLOOMING', 'SEPTETTE');
115+
expect : TStrArray = ('FOBS', 'RULE', 'ATOP', 'CLOT', 'TIME', 'UNIT', 'RENT', 'EDGE');
116+
begin
117+
TapAssertTrue(Self, 'rectangle', expect, Transpose.transpose(lines));
118+
end;
119+
120+
// b80badc9-057e-4543-bd07-ce1296a1ea2c
121+
procedure TransposeTest.triangle;
122+
const
123+
lines : TStrArray = ('T', 'EE', 'AAA', 'SSSS', 'EEEEE', 'RRRRRR');
124+
expect : TStrArray = ('TEASER', ' EASER', ' ASER', ' SER', ' ER', ' R');
125+
begin
126+
TapAssertTrue(Self, 'triangle', expect, Transpose.transpose(lines));
127+
end;
128+
129+
// 76acfd50-5596-4d05-89f1-5116328a7dd9
130+
procedure TransposeTest.jagged_triangle;
131+
const
132+
lines : TStrArray = ('11', '2', '3333', '444', '555555', '66666');
133+
expect : TStrArray = ('123456', '1 3456', ' 3456', ' 3 56', ' 56', ' 5');
134+
begin
135+
TapAssertTrue(Self, 'jagged triangle', expect, Transpose.transpose(lines));
136+
end;
137+
138+
initialization
139+
RegisterTest(TransposeTest);
140+
141+
end.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
unit Transpose;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TStrArray = array of string;
9+
10+
function transpose(const lines : TStrArray) : TStrArray;
11+
12+
implementation
13+
14+
uses SysUtils;
15+
16+
function transpose(const lines : TStrArray) : TStrArray;
17+
begin
18+
19+
raise ENotImplemented.Create('Please implement your solution.'); result := nil; SetLength(result, length(lines));
20+
21+
end;
22+
23+
end.

0 commit comments

Comments
 (0)