Skip to content

Commit dd373c9

Browse files
Add transpose exercise (#709)
* scaffolding * update config.json * implement tests * define contents of starter file * add leiningen and deps project files * add example solution * update main config.json * refactor example solution * rename test functions * move example.clj to .meta folder * Update exercises/practice/transpose/.meta/config.json Co-authored-by: Erik Schierboom <[email protected]> --------- Co-authored-by: Erik Schierboom <[email protected]>
1 parent 53bc55d commit dd373c9

File tree

9 files changed

+361
-0
lines changed

9 files changed

+361
-0
lines changed

config.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,19 @@
12221222
"conditionals"
12231223
]
12241224
},
1225+
{
1226+
"slug": "transpose",
1227+
"name": "Transpose",
1228+
"uuid": "a5dbea81-e24b-453e-bec9-bae4549ae52a",
1229+
"practices": [],
1230+
"prerequisites": [
1231+
"basics",
1232+
"chars",
1233+
"strings",
1234+
"vectors"
1235+
],
1236+
"difficulty": 5
1237+
},
12251238
{
12261239
"slug": "wordy",
12271240
"name": "Wordy",
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+
"tasxatzial"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/transpose.clj"
8+
],
9+
"test": [
10+
"test/transpose_test.clj"
11+
],
12+
"example": [
13+
".meta/example.clj"
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(ns transpose)
2+
3+
(defn pad-lines
4+
[lines]
5+
(if-let [reversed-lines (seq (reverse lines))]
6+
(loop [rev-lines (rest reversed-lines)
7+
padded-lines [(first reversed-lines)]]
8+
(if (seq rev-lines)
9+
(let [diff (- (.length (peek padded-lines)) (.length (first rev-lines)))
10+
new-padded-line (str (first rev-lines) (apply str (repeat diff " ")))]
11+
(recur (rest rev-lines) (conj padded-lines new-padded-line)))
12+
(rseq padded-lines)))
13+
[]))
14+
15+
(defn transpose
16+
[s]
17+
(loop [result []
18+
padded-lines (-> s clojure.string/split-lines pad-lines)]
19+
(if (seq (first padded-lines))
20+
(recur (conj result (map first padded-lines)) (map rest padded-lines))
21+
(clojure.string/join "\n" (map #(apply str %) result)))))
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"

exercises/practice/transpose/deps.edn

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{:aliases {:test {:extra-paths ["test"]
2+
:extra-deps {io.github.cognitect-labs/test-runner
3+
{:git/url "https://github.com/cognitect-labs/test-runner.git"
4+
:sha "705ad25bbf0228b1c38d0244a36001c2987d7337"}}
5+
:main-opts ["-m" "cognitect.test-runner"]
6+
:exec-fn cognitect.test-runner.api/test}}}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(defproject transpose "0.1.0-SNAPSHOT"
2+
:description "transpose exercise."
3+
:url "https://github.com/exercism/clojure/tree/master/exercises/transpose"
4+
:dependencies [[org.clojure/clojure "1.11.1"]])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(ns transpose)
2+
3+
(defn transpose
4+
"Given a string, it returns the transposed version"
5+
[s]
6+
;; function body
7+
)
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
(ns transpose-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
transpose))
4+
5+
(defn join-with-line-separator
6+
[coll]
7+
(clojure.string/join "\n" coll))
8+
9+
(deftest transpose_test_1
10+
(testing "empty string"
11+
(is (= (join-with-line-separator [""])
12+
(transpose/transpose
13+
(join-with-line-separator [""]))))))
14+
15+
(deftest transpose_test_2
16+
(testing "two characters in a row"
17+
(is (= (join-with-line-separator ["A"
18+
"1"])
19+
(transpose/transpose
20+
(join-with-line-separator ["A1"]))))))
21+
22+
(deftest transpose_test_3
23+
(testing "two characters in a column"
24+
(is (= (join-with-line-separator ["A1"])
25+
(transpose/transpose
26+
(join-with-line-separator ["A"
27+
"1"]))))))
28+
29+
(deftest transpose_test_4
30+
(testing "simple"
31+
(is (= (join-with-line-separator ["A1"
32+
"B2"
33+
"C3"])
34+
(transpose/transpose
35+
(join-with-line-separator ["ABC"
36+
"123"]))))))
37+
38+
(deftest transpose_test_5
39+
(testing "single line"
40+
(is (= (join-with-line-separator ["S"
41+
"i"
42+
"n"
43+
"g"
44+
"l"
45+
"e"
46+
" "
47+
"l"
48+
"i"
49+
"n"
50+
"e"
51+
"."])
52+
(transpose/transpose
53+
(join-with-line-separator ["Single line."]))))))
54+
55+
(deftest transpose_test_6
56+
(testing "first line longer than second line"
57+
(is (= (join-with-line-separator ["TT"
58+
"hh"
59+
"ee"
60+
" "
61+
"ff"
62+
"oi"
63+
"uf"
64+
"rt"
65+
"th"
66+
"h "
67+
" l"
68+
"li"
69+
"in"
70+
"ne"
71+
"e."
72+
"."])
73+
(transpose/transpose
74+
(join-with-line-separator ["The fourth line."
75+
"The fifth line."]))))))
76+
77+
(deftest transpose_test_7
78+
(testing "second line longer than first line"
79+
(is (= (join-with-line-separator ["TT"
80+
"hh"
81+
"ee"
82+
" "
83+
"fs"
84+
"ie"
85+
"rc"
86+
"so"
87+
"tn"
88+
" d"
89+
"l "
90+
"il"
91+
"ni"
92+
"en"
93+
".e"
94+
" ."])
95+
(transpose/transpose
96+
(join-with-line-separator ["The first line.",
97+
"The second line."]))))))
98+
99+
(deftest transpose_test_8
100+
(testing "mixed line length"
101+
(is (= (join-with-line-separator ["TAAA"
102+
"h "
103+
"elll"
104+
" ooi"
105+
"lnnn"
106+
"ogge"
107+
"n e."
108+
"glr"
109+
"ei "
110+
"snl"
111+
"tei"
112+
" .n"
113+
"l e"
114+
"i ."
115+
"n"
116+
"e"
117+
"."])
118+
(transpose/transpose
119+
(join-with-line-separator ["The longest line."
120+
"A long line."
121+
"A longer line."
122+
"A line."]))))))
123+
124+
(deftest transpose_test_9
125+
(testing "square"
126+
(is (= (join-with-line-separator ["HEART"
127+
"EMBER"
128+
"ABUSE"
129+
"RESIN"
130+
"TREND"])
131+
(transpose/transpose
132+
(join-with-line-separator ["HEART"
133+
"EMBER"
134+
"ABUSE"
135+
"RESIN"
136+
"TREND"]))))))
137+
138+
(deftest transpose_test_10
139+
(testing "rectangle"
140+
(is (= (join-with-line-separator ["FOBS"
141+
"RULE"
142+
"ATOP"
143+
"CLOT"
144+
"TIME"
145+
"UNIT"
146+
"RENT"
147+
"EDGE"])
148+
(transpose/transpose
149+
(join-with-line-separator ["FRACTURE"
150+
"OUTLINED"
151+
"BLOOMING"
152+
"SEPTETTE"]))))))
153+
154+
(deftest transpose_test_11
155+
(testing "triangle"
156+
(is (= (join-with-line-separator ["TEASER"
157+
" EASER"
158+
" ASER"
159+
" SER"
160+
" ER"
161+
" R"])
162+
(transpose/transpose
163+
(join-with-line-separator ["T"
164+
"EE"
165+
"AAA"
166+
"SSSS"
167+
"EEEEE"
168+
"RRRRRR"]))))))
169+
170+
(deftest transpose_test_12
171+
(testing "jagged triangle"
172+
(is (= (join-with-line-separator ["123456"
173+
"1 3456"
174+
" 3456"
175+
" 3 56"
176+
" 56"
177+
" 5"])
178+
(transpose/transpose
179+
(join-with-line-separator ["11"
180+
"2"
181+
"3333"
182+
"444"
183+
"555555"
184+
"66666"]))))))

0 commit comments

Comments
 (0)