Skip to content

Commit 547501f

Browse files
Add transpose exercise (#520)
1 parent b47bd86 commit 547501f

7 files changed

Lines changed: 359 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,14 @@
652652
"prerequisites": [],
653653
"difficulty": 4
654654
},
655+
{
656+
"slug": "transpose",
657+
"name": "Transpose",
658+
"uuid": "486e4c05-1741-4b33-8191-86bd3f50149d",
659+
"practices": [],
660+
"prerequisites": [],
661+
"difficulty": 4
662+
},
655663
{
656664
"slug": "twelve-days",
657665
"name": "Twelve Days",
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.el"
8+
],
9+
"test": [
10+
"transpose-test.el"
11+
],
12+
"example": [
13+
".meta/example.el"
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
;;; transpose.el --- Transpose (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
(require 'cl-lib)
8+
9+
(defun column (col reversed height)
10+
(cond ((null reversed) "")
11+
((<= (length (car reversed)) col) (column col (cdr reversed) (1- height)))
12+
(t (let ((word (make-string height ?\s)))
13+
(cl-loop
14+
for row from (1- height) downto 0
15+
for line = (car reversed)
16+
17+
when (< col (length line))
18+
do (aset word row (aref line col))
19+
20+
do (setq reversed (cdr reversed)))
21+
word))))
22+
23+
(defun transpose (lines)
24+
(when lines
25+
(let ((height (length lines))
26+
(width (apply #'max (mapcar #'length lines)))
27+
(reversed (reverse lines)))
28+
(cl-loop for col from 0 below width
29+
collect (column col reversed height)))))
30+
31+
(provide 'transpose)
32+
;;; transpose.el ends here
33+
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: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
;;; transpose-test.el --- Tests for Transpose (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(load-file "transpose.el")
9+
(declare-function transpose "transpose.el" (lines))
10+
11+
12+
(ert-deftest empty-string ()
13+
(let ((expected '()))
14+
(should (equal (transpose '()) expected))))
15+
16+
17+
(ert-deftest two-characters-in-a-row ()
18+
(let ((expected '("A"
19+
"1")))
20+
(should (equal (transpose '("A1")) expected))))
21+
22+
23+
(ert-deftest two-characters-in-a-column ()
24+
(let ((expected '("A1")))
25+
(should (equal (transpose '("A"
26+
"1")) expected))))
27+
28+
29+
(ert-deftest simple ()
30+
(let ((expected '("A1"
31+
"B2"
32+
"C3")))
33+
(should (equal (transpose '("ABC"
34+
"123")) expected))))
35+
36+
37+
(ert-deftest single-line ()
38+
(let ((expected '("S"
39+
"i"
40+
"n"
41+
"g"
42+
"l"
43+
"e"
44+
" "
45+
"l"
46+
"i"
47+
"n"
48+
"e"
49+
".")))
50+
(should (equal (transpose '("Single line.")) expected))))
51+
52+
53+
(ert-deftest first-line-longer-than-second-line ()
54+
(let ((expected '("TT"
55+
"hh"
56+
"ee"
57+
" "
58+
"ff"
59+
"oi"
60+
"uf"
61+
"rt"
62+
"th"
63+
"h "
64+
" l"
65+
"li"
66+
"in"
67+
"ne"
68+
"e."
69+
".")))
70+
(should (equal (transpose '("The fourth line."
71+
"The fifth line.")) expected))))
72+
73+
74+
(ert-deftest second-line-longer-than-first-line ()
75+
(let ((expected '("TT"
76+
"hh"
77+
"ee"
78+
" "
79+
"fs"
80+
"ie"
81+
"rc"
82+
"so"
83+
"tn"
84+
" d"
85+
"l "
86+
"il"
87+
"ni"
88+
"en"
89+
".e"
90+
" .")))
91+
(should (equal (transpose '("The first line."
92+
"The second line.")) expected))))
93+
94+
95+
(ert-deftest mixed-line-length ()
96+
(let ((expected '("TAAA"
97+
"h "
98+
"elll"
99+
" ooi"
100+
"lnnn"
101+
"ogge"
102+
"n e."
103+
"glr"
104+
"ei "
105+
"snl"
106+
"tei"
107+
" .n"
108+
"l e"
109+
"i ."
110+
"n"
111+
"e"
112+
".")))
113+
(should (equal (transpose '("The longest line."
114+
"A long line."
115+
"A longer line."
116+
"A line.")) expected))))
117+
118+
119+
(ert-deftest square ()
120+
(let ((expected '("HEART"
121+
"EMBER"
122+
"ABUSE"
123+
"RESIN"
124+
"TREND")))
125+
(should (equal (transpose '("HEART"
126+
"EMBER"
127+
"ABUSE"
128+
"RESIN"
129+
"TREND")) expected))))
130+
131+
132+
(ert-deftest rectangle ()
133+
(let ((expected '("FOBS"
134+
"RULE"
135+
"ATOP"
136+
"CLOT"
137+
"TIME"
138+
"UNIT"
139+
"RENT"
140+
"EDGE")))
141+
(should (equal (transpose '("FRACTURE"
142+
"OUTLINED"
143+
"BLOOMING"
144+
"SEPTETTE")) expected))))
145+
146+
147+
(ert-deftest triangle ()
148+
(let ((expected '("TEASER"
149+
" EASER"
150+
" ASER"
151+
" SER"
152+
" ER"
153+
" R")))
154+
(should (equal (transpose '("T"
155+
"EE"
156+
"AAA"
157+
"SSSS"
158+
"EEEEE"
159+
"RRRRRR")) expected))))
160+
161+
162+
(ert-deftest jagged-triangle ()
163+
(let ((expected '("123456"
164+
"1 3456"
165+
" 3456"
166+
" 3 56"
167+
" 56"
168+
" 5")))
169+
(should (equal (transpose '("11"
170+
"2"
171+
"3333"
172+
"444"
173+
"555555"
174+
"66666")) expected))))
175+
176+
177+
(provide 'transpose-test)
178+
;;; transpose-test.el ends here
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
;;; transpose.el --- Transpose (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defun transpose (lines)
9+
(error "Delete this S-Expression and write your own implementation"))
10+
11+
12+
(provide 'transpose)
13+
;;; transpose.el ends here
14+

0 commit comments

Comments
 (0)