Skip to content

Commit 9fc18d5

Browse files
Add transpose exercise (#508)
1 parent 46367ff commit 9fc18d5

File tree

7 files changed

+468
-0
lines changed

7 files changed

+468
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,14 @@
10031003
"prerequisites": [],
10041004
"difficulty": 5
10051005
},
1006+
{
1007+
"slug": "transpose",
1008+
"name": "Transpose",
1009+
"uuid": "3cad82e8-b201-4f4c-81eb-8239521be255",
1010+
"practices": [],
1011+
"prerequisites": [],
1012+
"difficulty": 5
1013+
},
10061014
{
10071015
"slug": "variable-length-quantity",
10081016
"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.zig"
8+
],
9+
"test": [
10+
"test_transpose.zig"
11+
],
12+
"example": [
13+
".meta/example.zig"
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const std = @import("std");
2+
const mem = std.mem;
3+
4+
fn free(allocator: mem.Allocator, array_list: *std.ArrayList([]u8)) void {
5+
for (array_list.items) |item| {
6+
allocator.free(item);
7+
}
8+
array_list.deinit(allocator);
9+
}
10+
11+
pub fn transpose(allocator: mem.Allocator, lines: []const []const u8) mem.Allocator.Error![][]u8 {
12+
var longest: usize = 0;
13+
for (lines) |line| {
14+
if (longest < line.len) {
15+
longest = line.len;
16+
}
17+
}
18+
19+
var array_list = try std.ArrayList([]u8).initCapacity(allocator, longest);
20+
errdefer free(allocator, &array_list);
21+
for (0..longest) |i| {
22+
var line = try std.ArrayList(u8).initCapacity(allocator, lines.len);
23+
defer line.deinit(allocator);
24+
for (0..lines.len) |j| {
25+
if (lines[j].len <= i) {
26+
continue;
27+
}
28+
29+
while (line.items.len < j) {
30+
line.appendAssumeCapacity(' ');
31+
}
32+
line.appendAssumeCapacity(lines[j][i]);
33+
}
34+
array_list.appendAssumeCapacity(try line.toOwnedSlice(allocator));
35+
}
36+
return array_list.toOwnedSlice(allocator);
37+
}
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"

0 commit comments

Comments
 (0)