Skip to content

Add transpose exercise #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "transpose",
"name": "Transpose",
"uuid": "dad518d5-c579-4f7c-8a62-26b81425f173",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "yacht",
"name": "Yacht",
Expand Down
61 changes: 61 additions & 0 deletions exercises/practice/transpose/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Instructions

Given an input text output it transposed.

Roughly explained, the transpose of a matrix:

```text
ABC
DEF
```

is given by:

```text
AD
BE
CF
```

Rows become columns and columns become rows.
See [transpose][].

If the input has rows of different lengths, this is to be solved as follows:

- Pad to the left with spaces.
- Don't pad to the right.

Therefore, transposing this matrix:

```text
ABC
DE
```

results in:

```text
AD
BE
C
```

And transposing:

```text
AB
DEF
```

results in:

```text
AD
BE
F
```

In general, all characters from the input should also be present in the transposed output.
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).

[transpose]: https://en.wikipedia.org/wiki/Transpose
19 changes: 19 additions & 0 deletions exercises/practice/transpose/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"transpose.v"
],
"test": [
"run_test.v"
],
"example": [
".meta/example.v"
]
},
"blurb": "Take input text and output it transposed.",
"source": "Reddit r/dailyprogrammer challenge #270 [Easy].",
"source_url": "https://web.archive.org/web/20230630051421/https://old.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text/"
}
31 changes: 31 additions & 0 deletions exercises/practice/transpose/.meta/example.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module main

import arrays

fn transpose(lines []string) []string {
mut max_line_len := 0
for line in lines {
if max_line_len < line.len {
max_line_len = line.len
}
}

mut result := []string{len: max_line_len}
for i in 0 .. max_line_len {
mut buffer := []u8{cap: lines.len}
for j in 0 .. (lines.len) {
if lines[j].len <= i {
continue
}
for buffer.len < j {
buffer << ` `
}

buffer << lines[j][i]
}

result[i] = buffer.bytestr()
}

return result
}
39 changes: 39 additions & 0 deletions exercises/practice/transpose/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.

[404b7262-c050-4df0-a2a2-0cb06cd6a821]
description = "empty string"

[a89ce8a3-c940-4703-a688-3ea39412fbcb]
description = "two characters in a row"

[855bb6ae-4180-457c-abd0-ce489803ce98]
description = "two characters in a column"

[5ceda1c0-f940-441c-a244-0ced197769c8]
description = "simple"

[a54675dd-ae7d-4a58-a9c4-0c20e99a7c1f]
description = "single line"

[0dc2ec0b-549d-4047-aeeb-8029fec8d5c5]
description = "first line longer than second line"

[984e2ec3-b3d3-4b53-8bd6-96f5ef404102]
description = "second line longer than first line"

[eccd3784-45f0-4a3f-865a-360cb323d314]
description = "mixed line length"

[85b96b3f-d00c-4f80-8ca2-c8a5c9216c2d]
description = "square"

[b9257625-7a53-4748-8863-e08e9d27071d]
description = "rectangle"

[b80badc9-057e-4543-bd07-ce1296a1ea2c]
description = "triangle"

[76acfd50-5596-4d05-89f1-5116328a7dd9]
description = "jagged triangle"
222 changes: 222 additions & 0 deletions exercises/practice/transpose/run_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
module main

fn test_empty_string() {
lines := []string{}
expect := []string{}
assert transpose(lines) == expect
}

fn test_two_characters_in_a_row() {
lines := [
'A1',
]
expect := [
'A',
'1',
]
assert transpose(lines) == expect
}

fn test_two_characters_in_a_column() {
lines := [
'A',
'1',
]
expect := [
'A1',
]
assert transpose(lines) == expect
}

fn test_simple() {
lines := [
'ABC',
'123',
]
expect := [
'A1',
'B2',
'C3',
]
assert transpose(lines) == expect
}

fn test_single_line() {
lines := [
'Single line.',
]
expect := [
'S',
'i',
'n',
'g',
'l',
'e',
' ',
'l',
'i',
'n',
'e',
'.',
]
assert transpose(lines) == expect
}

fn test_first_line_longer_than_second_line() {
lines := [
'The fourth line.',
'The fifth line.',
]
expect := [
'TT',
'hh',
'ee',
' ',
'ff',
'oi',
'uf',
'rt',
'th',
'h ',
' l',
'li',
'in',
'ne',
'e.',
'.',
]
assert transpose(lines) == expect
}

fn test_second_line_longer_than_first_line() {
lines := [
'The first line.',
'The second line.',
]
expect := [
'TT',
'hh',
'ee',
' ',
'fs',
'ie',
'rc',
'so',
'tn',
' d',
'l ',
'il',
'ni',
'en',
'.e',
' .',
]
assert transpose(lines) == expect
}

fn test_mixed_line_length() {
lines := [
'The longest line.',
'A long line.',
'A longer line.',
'A line.',
]
expect := [
'TAAA',
'h ',
'elll',
' ooi',
'lnnn',
'ogge',
'n e.',
'glr',
'ei ',
'snl',
'tei',
' .n',
'l e',
'i .',
'n',
'e',
'.',
]
assert transpose(lines) == expect
}

fn test_square() {
lines := [
'HEART',
'EMBER',
'ABUSE',
'RESIN',
'TREND',
]
expect := [
'HEART',
'EMBER',
'ABUSE',
'RESIN',
'TREND',
]
assert transpose(lines) == expect
}

fn test_rectangle() {
lines := [
'FRACTURE',
'OUTLINED',
'BLOOMING',
'SEPTETTE',
]
expect := [
'FOBS',
'RULE',
'ATOP',
'CLOT',
'TIME',
'UNIT',
'RENT',
'EDGE',
]
assert transpose(lines) == expect
}

fn test_triangle() {
lines := [
'T',
'EE',
'AAA',
'SSSS',
'EEEEE',
'RRRRRR',
]
expect := [
'TEASER',
' EASER',
' ASER',
' SER',
' ER',
' R',
]
assert transpose(lines) == expect
}

fn test_jagged_triangle() {
lines := [
'11',
'2',
'3333',
'444',
'555555',
'66666',
]
expect := [
'123456',
'1 3456',
' 3456',
' 3 56',
' 56',
' 5',
]
assert transpose(lines) == expect
}
4 changes: 4 additions & 0 deletions exercises/practice/transpose/transpose.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module main

fn transpose(lines []string) []string {
}
Loading