Skip to content

Commit 82556e4

Browse files
authored
Add transpose (#134)
1 parent a258f20 commit 82556e4

10 files changed

Lines changed: 382 additions & 1 deletion

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,14 @@
570570
"prerequisites": [],
571571
"difficulty": 4
572572
},
573+
{
574+
"slug": "transpose",
575+
"name": "Transpose",
576+
"uuid": "c71d962b-61f1-4a41-9b73-20e262ae7469",
577+
"practices": [],
578+
"prerequisites": [],
579+
"difficulty": 4
580+
},
573581
{
574582
"slug": "twelve-days",
575583
"name": "Twelve Days",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
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+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"transpose.moon"
8+
],
9+
"test": [
10+
"transpose_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
is_empty = (t) -> not next t
2+
3+
pad_end = (str, wid) -> string.format "%-#{wid}s", str
4+
5+
trim_end = (str) -> str\gsub '%s+$', ''
6+
7+
{
8+
transpose: (input) ->
9+
return {} if is_empty input
10+
11+
max = math.max table.unpack [#line for line in *input]
12+
padded = [pad_end line, max for line in *input]
13+
14+
transposed = {}
15+
for i = 1, #padded[1]
16+
row = [line\sub i, i for line in *padded]
17+
line = trim_end table.concat(row, '')
18+
table.insert transposed, line
19+
20+
wid = #transposed[#transposed]
21+
for i = #transposed - 1, 1, -1
22+
if #transposed[i] < wid
23+
transposed[i] = pad_end transposed[i], wid
24+
else
25+
wid = #transposed[i]
26+
27+
transposed
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
string_list = (list, level) ->
2+
if #list <= 2
3+
"{#{table.concat [quote elem for elem in *list], ', '}}"
4+
else
5+
instrs = [indent quote(elem) .. ',', level + 1 for elem in *list]
6+
table.insert instrs, 1, '{'
7+
table.insert instrs, indent('}', level)
8+
table.concat instrs, '\n'
9+
10+
{
11+
module_imports: {'transpose'},
12+
13+
generate_test: (case, level) ->
14+
lines = {
15+
"input = #{string_list case.input.lines, level}",
16+
"expected = #{string_list case.expected, level}",
17+
"assert.are.same expected, transpose input"
18+
}
19+
table.concat [indent line, level for line in *lines], '\n'
20+
}
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
transpose: (input) ->
3+
error 'Implement me!'
4+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import transpose from require 'transpose'
2+
3+
describe 'transpose', ->
4+
it 'empty string', ->
5+
input = {}
6+
expected = {}
7+
assert.are.same expected, transpose input
8+
9+
pending 'two characters in a row', ->
10+
input = {'A1'}
11+
expected = {'A', '1'}
12+
assert.are.same expected, transpose input
13+
14+
pending 'two characters in a column', ->
15+
input = {'A', '1'}
16+
expected = {'A1'}
17+
assert.are.same expected, transpose input
18+
19+
pending 'simple', ->
20+
input = {'ABC', '123'}
21+
expected = {
22+
'A1',
23+
'B2',
24+
'C3',
25+
}
26+
assert.are.same expected, transpose input
27+
28+
pending 'single line', ->
29+
input = {'Single line.'}
30+
expected = {
31+
'S',
32+
'i',
33+
'n',
34+
'g',
35+
'l',
36+
'e',
37+
' ',
38+
'l',
39+
'i',
40+
'n',
41+
'e',
42+
'.',
43+
}
44+
assert.are.same expected, transpose input
45+
46+
pending 'first line longer than second line', ->
47+
input = {'The fourth line.', 'The fifth line.'}
48+
expected = {
49+
'TT',
50+
'hh',
51+
'ee',
52+
' ',
53+
'ff',
54+
'oi',
55+
'uf',
56+
'rt',
57+
'th',
58+
'h ',
59+
' l',
60+
'li',
61+
'in',
62+
'ne',
63+
'e.',
64+
'.',
65+
}
66+
assert.are.same expected, transpose input
67+
68+
pending 'second line longer than first line', ->
69+
input = {'The first line.', 'The second line.'}
70+
expected = {
71+
'TT',
72+
'hh',
73+
'ee',
74+
' ',
75+
'fs',
76+
'ie',
77+
'rc',
78+
'so',
79+
'tn',
80+
' d',
81+
'l ',
82+
'il',
83+
'ni',
84+
'en',
85+
'.e',
86+
' .',
87+
}
88+
assert.are.same expected, transpose input
89+
90+
pending 'mixed line length', ->
91+
input = {
92+
'The longest line.',
93+
'A long line.',
94+
'A longer line.',
95+
'A line.',
96+
}
97+
expected = {
98+
'TAAA',
99+
'h ',
100+
'elll',
101+
' ooi',
102+
'lnnn',
103+
'ogge',
104+
'n e.',
105+
'glr',
106+
'ei ',
107+
'snl',
108+
'tei',
109+
' .n',
110+
'l e',
111+
'i .',
112+
'n',
113+
'e',
114+
'.',
115+
}
116+
assert.are.same expected, transpose input
117+
118+
pending 'square', ->
119+
input = {
120+
'HEART',
121+
'EMBER',
122+
'ABUSE',
123+
'RESIN',
124+
'TREND',
125+
}
126+
expected = {
127+
'HEART',
128+
'EMBER',
129+
'ABUSE',
130+
'RESIN',
131+
'TREND',
132+
}
133+
assert.are.same expected, transpose input
134+
135+
pending 'rectangle', ->
136+
input = {
137+
'FRACTURE',
138+
'OUTLINED',
139+
'BLOOMING',
140+
'SEPTETTE',
141+
}
142+
expected = {
143+
'FOBS',
144+
'RULE',
145+
'ATOP',
146+
'CLOT',
147+
'TIME',
148+
'UNIT',
149+
'RENT',
150+
'EDGE',
151+
}
152+
assert.are.same expected, transpose input
153+
154+
pending 'triangle', ->
155+
input = {
156+
'T',
157+
'EE',
158+
'AAA',
159+
'SSSS',
160+
'EEEEE',
161+
'RRRRRR',
162+
}
163+
expected = {
164+
'TEASER',
165+
' EASER',
166+
' ASER',
167+
' SER',
168+
' ER',
169+
' R',
170+
}
171+
assert.are.same expected, transpose input
172+
173+
pending 'jagged triangle', ->
174+
input = {
175+
'11',
176+
'2',
177+
'3333',
178+
'444',
179+
'555555',
180+
'66666',
181+
}
182+
expected = {
183+
'123456',
184+
'1 3456',
185+
' 3456',
186+
' 3 56',
187+
' 56',
188+
' 5',
189+
}
190+
assert.are.same expected, transpose input

0 commit comments

Comments
 (0)