Skip to content
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

[transpose] Implementation #629

Merged
merged 10 commits into from Jun 25, 2023
9 changes: 9 additions & 0 deletions config.json
Expand Up @@ -580,6 +580,15 @@
"prerequisites": [],
"difficulty": 1,
"topics": []
},
{
"slug": "transpose",
"name": "Transpose",
"uuid": "1071ff62-8951-4176-bfcf-0f36b0b122a1",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
}
]
},
Expand Down
61 changes: 61 additions & 0 deletions exercises/practice/transpose/.docs/instructions.md
@@ -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
20 changes: 20 additions & 0 deletions exercises/practice/transpose/.meta/config.json
@@ -0,0 +1,20 @@
{
"authors": [
"habere-et-dispertire",
"m-dango"
],
"files": {
"solution": [
"Transpose.rakumod"
],
"test": [
"transpose.rakutest"
],
"example": [
".meta/solutions/Transpose.rakumod"
]
},
"blurb": "Take input text and output it transposed.",
"source": "Reddit r/dailyprogrammer challenge #270 [Easy].",
"source_url": "https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text"
}
12 changes: 12 additions & 0 deletions exercises/practice/transpose/.meta/solutions/Transpose.rakumod
@@ -0,0 +1,12 @@
unit module Transpose;

sub transpose ($text) is export {
my @matrix;

for $text.lines.pairs
-> $line {
@matrix[ .key ; $line.key ] = .value for $line.value.comb.pairs
}

return @matrix.deepmap( { .defined ?? $_ !! ' ' } ).map( *.join ).join: "\n";
}
34 changes: 34 additions & 0 deletions exercises/practice/transpose/.meta/template-data.yaml
@@ -0,0 +1,34 @@
unit: module

properties:
transpose:
test: |-
sprintf(q:to/END/, (given %case<input><lines> {$_ ?? .append('').join("\n ") !! ""}) , (given %case<expected> {$_ ?? .append('').join("\n ") !! ""}), %case<description>.raku);
cmp-ok(
transpose(q:to/---/.trim),
%s---

"eq",

q:to/---/.trim,
%s---

%s,
);
END

example: |-
sub transpose ($text) is export {
my @matrix;

for $text.lines.pairs
-> $line {
@matrix[ .key ; $line.key ] = .value for $line.value.comb.pairs
}

return @matrix.deepmap( { .defined ?? $_ !! ' ' } ).map( *.join ).join: "\n";
}

stub: |-
sub transpose ($text) is export {
}
46 changes: 46 additions & 0 deletions exercises/practice/transpose/.meta/tests.toml
@@ -0,0 +1,46 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via 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"
4 changes: 4 additions & 0 deletions exercises/practice/transpose/Transpose.rakumod
@@ -0,0 +1,4 @@
unit module Transpose;

sub transpose ($text) is export {
}