Skip to content

Commit 4853b11

Browse files
authored
Merge pull request exercism#604 from Smarticles101/transpose
transpose: add to track
2 parents 9b30aee + 4a9ea26 commit 4853b11

6 files changed

Lines changed: 331 additions & 1 deletion

File tree

config.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@
284284

285285
]
286286
},
287+
{
288+
"slug": "transpose",
289+
"difficulty": 6,
290+
"topics": [
291+
292+
]
293+
},
287294
{
288295
"slug": "beer-song",
289296
"difficulty": 6,
@@ -470,7 +477,7 @@
470477
"slug": "list-ops",
471478
"difficulty": 1,
472479
"topics": [
473-
480+
474481
]
475482
}
476483
],

exercises/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ include 'space-age'
6565
include 'strain'
6666
include 'sublist'
6767
include 'sum-of-multiples'
68+
include 'transpose'
6869
include 'triangle'
6970
include 'trinary'
7071
include 'twelve-days'

exercises/transpose/build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
test {
13+
testLogging {
14+
exceptionFormat = 'full'
15+
events = ["passed", "failed", "skipped"]
16+
}
17+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class Transpose {
2+
public String transpose(String toTranspose) {
3+
4+
StringBuilder transposedForm = new StringBuilder();
5+
String[] rows = toTranspose.split("\n");
6+
7+
int maxRowSize = 0;
8+
9+
for (int i = 0; i < rows.length; i++) {
10+
if (rows[i].length() > maxRowSize) {
11+
maxRowSize = rows[i].length();
12+
}
13+
}
14+
15+
padRows(rows);
16+
17+
for (int newRowOldCol = 0; newRowOldCol < maxRowSize; newRowOldCol++) {
18+
for (int newColOldRow = 0; newColOldRow < rows.length; newColOldRow++) {
19+
if (newRowOldCol < rows[newColOldRow].length()) {
20+
transposedForm.append(rows[newColOldRow].charAt(newRowOldCol));
21+
}
22+
}
23+
24+
if (newRowOldCol != maxRowSize - 1) {
25+
transposedForm.append("\n");
26+
}
27+
}
28+
29+
return transposedForm.toString();
30+
}
31+
32+
private void padRows(String[] rows) {
33+
for (int i = 0; i < rows.length; i++) {
34+
for (int j = i; j < rows.length; j++) {
35+
if(rows[j].length() > rows[i].length()) {
36+
rows[i] = String.format("%-" + rows[j].length() + "s", rows[i]);
37+
}
38+
}
39+
}
40+
}
41+
}

exercises/transpose/src/main/java/.keep

Whitespace-only changes.
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
import org.junit.Ignore;
2+
import org.junit.Test;
3+
import org.junit.Before;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class TransposeTest {
8+
private Transpose transpose;
9+
10+
@Before
11+
public void setup() {
12+
transpose = new Transpose();
13+
}
14+
15+
@Test
16+
public void emptyString() {
17+
String input = "";
18+
19+
String expected = "";
20+
21+
assertEquals(expected, transpose.transpose(input));
22+
}
23+
24+
@Test
25+
@Ignore("Remove to run test")
26+
public void twoCharsInARow() {
27+
String input = "A1";
28+
29+
String expected = "A" +
30+
"\n1";
31+
32+
assertEquals(expected, transpose.transpose(input));
33+
}
34+
35+
@Test
36+
@Ignore("Remove to run test")
37+
public void twoCharsInAColumn() {
38+
String input = "A\n" +
39+
"1";
40+
41+
String expected = "A1";
42+
43+
assertEquals(expected, transpose.transpose(input));
44+
}
45+
46+
@Test
47+
@Ignore("Remove to run test")
48+
public void simpleMatrix() {
49+
String input = "ABC\n" +
50+
"123";
51+
52+
String expected = "A1\n" +
53+
"B2\n" +
54+
"C3";
55+
56+
assertEquals(expected, transpose.transpose(input));
57+
}
58+
59+
@Test
60+
@Ignore("Remove to run test")
61+
public void singleLine() {
62+
String input = "Single line.";
63+
64+
String expected = "S\n" +
65+
"i\n" +
66+
"n\n" +
67+
"g\n" +
68+
"l\n" +
69+
"e\n" +
70+
" \n" +
71+
"l\n" +
72+
"i\n" +
73+
"n\n" +
74+
"e\n" +
75+
".";
76+
77+
assertEquals(expected, transpose.transpose(input));
78+
}
79+
80+
@Test
81+
@Ignore("Remove to run test")
82+
public void firstLineLongerThanSecond() {
83+
String input = "The fourth line.\n" +
84+
"The fifth line.";
85+
86+
String expected = "TT\n" +
87+
"hh\n" +
88+
"ee\n" +
89+
" \n" +
90+
"ff\n" +
91+
"oi\n" +
92+
"uf\n" +
93+
"rt\n" +
94+
"th\n" +
95+
"h \n" +
96+
" l\n" +
97+
"li\n" +
98+
"in\n" +
99+
"ne\n" +
100+
"e.\n" +
101+
".";
102+
103+
assertEquals(expected, transpose.transpose(input));
104+
}
105+
106+
@Test
107+
@Ignore("Remove to run test")
108+
public void secondLineLongerThanFirst() {
109+
String input = "The first line.\n" +
110+
"The second line.";
111+
112+
String expected = "TT\n" +
113+
"hh\n" +
114+
"ee\n" +
115+
" \n" +
116+
"fs\n" +
117+
"ie\n" +
118+
"rc\n" +
119+
"so\n" +
120+
"tn\n" +
121+
" d\n" +
122+
"l \n" +
123+
"il\n" +
124+
"ni\n" +
125+
"en\n" +
126+
".e\n" +
127+
" .";
128+
129+
assertEquals(expected, transpose.transpose(input));
130+
}
131+
132+
@Test
133+
@Ignore("Remove to run test")
134+
public void square() {
135+
String input = "HEART\n" +
136+
"EMBER\n" +
137+
"ABUSE\n" +
138+
"RESIN\n" +
139+
"TREND";
140+
141+
String expected = "HEART\n" +
142+
"EMBER\n" +
143+
"ABUSE\n" +
144+
"RESIN\n" +
145+
"TREND";
146+
147+
assertEquals(expected, transpose.transpose(input));
148+
}
149+
150+
@Test
151+
@Ignore("Remove to run test")
152+
public void rectangle() {
153+
String input = "FRACTURE\n" +
154+
"OUTLINED\n" +
155+
"BLOOMING\n" +
156+
"SEPTETTE";
157+
158+
String expected = "FOBS\n" +
159+
"RULE\n" +
160+
"ATOP\n" +
161+
"CLOT\n" +
162+
"TIME\n" +
163+
"UNIT\n" +
164+
"RENT\n" +
165+
"EDGE";
166+
167+
assertEquals(expected, transpose.transpose(input));
168+
}
169+
170+
@Test
171+
@Ignore("Remove to run test")
172+
public void triangle() {
173+
String input = "T\n" +
174+
"EE\n" +
175+
"AAA\n" +
176+
"SSSS\n" +
177+
"EEEEE\n" +
178+
"RRRRRR";
179+
180+
String expected = "TEASER\n" +
181+
" EASER\n" +
182+
" ASER\n" +
183+
" SER\n" +
184+
" ER\n" +
185+
" R";
186+
187+
assertEquals(expected, transpose.transpose(input));
188+
}
189+
190+
@Test
191+
@Ignore("Remove to run test")
192+
public void manyLines() {
193+
String input = "Chor. Two households, both alike in dignity,\n" +
194+
"In fair Verona, where we lay our scene,\n" +
195+
"From ancient grudge break to new mutiny,\n" +
196+
"Where civil blood makes civil hands unclean.\n" +
197+
"From forth the fatal loins of these two foes\n" +
198+
"A pair of star-cross'd lovers take their life;\n" +
199+
"Whose misadventur'd piteous overthrows\n" +
200+
"Doth with their death bury their parents' strife.\n" +
201+
"The fearful passage of their death-mark'd love,\n" +
202+
"And the continuance of their parents' rage,\n" +
203+
"Which, but their children's end, naught could remove,\n" +
204+
"Is now the two hours' traffic of our stage;\n" +
205+
"The which if you with patient ears attend,\n" +
206+
"What here shall miss, our toil shall strive to mend.";
207+
208+
String expected = "CIFWFAWDTAWITW\n" +
209+
"hnrhr hohnhshh\n" +
210+
"o oeopotedi ea\n" +
211+
"rfmrmash cn t\n" +
212+
".a e ie fthow \n" +
213+
" ia fr weh,whh\n" +
214+
"Trnco miae ie\n" +
215+
"w ciroitr btcr\n" +
216+
"oVivtfshfcuhhe\n" +
217+
" eeih a uote \n" +
218+
"hrnl sdtln is\n" +
219+
"oot ttvh tttfh\n" +
220+
"un bhaeepihw a\n" +
221+
"saglernianeoyl\n" +
222+
"e,ro -trsui ol\n" +
223+
"h uofcu sarhu \n" +
224+
"owddarrdan o m\n" +
225+
"lhg to'egccuwi\n" +
226+
"deemasdaeehris\n" +
227+
"sr als t ists\n" +
228+
",ebk 'phool'h,\n" +
229+
" reldi ffd \n" +
230+
"bweso tb rtpo\n" +
231+
"oea ileutterau\n" +
232+
"t kcnoorhhnatr\n" +
233+
"hl isvuyee'fi \n" +
234+
" atv es iisfet\n" +
235+
"ayoior trr ino\n" +
236+
"l lfsoh ecti\n" +
237+
"ion vedpn l\n" +
238+
"kuehtteieadoe \n" +
239+
"erwaharrar,fas\n" +
240+
" nekt te rh\n" +
241+
"ismdsehphnnosa\n" +
242+
"ncuse ra-tau l\n" +
243+
" et tormsural\n" +
244+
"dniuthwea'g t \n" +
245+
"iennwesnr hsts\n" +
246+
"g,ycoi tkrttet\n" +
247+
"n ,l r s'a anr\n" +
248+
"i ef 'dgcgdi\n" +
249+
"t aol eoe,v\n" +
250+
"y nei sl,u; e\n" +
251+
", .sf to l \n" +
252+
" e rv d t\n" +
253+
" ; ie o\n" +
254+
" f, r \n" +
255+
" e e m\n" +
256+
" . m e\n" +
257+
" o n\n" +
258+
" v d\n" +
259+
" e .\n" +
260+
" ,";
261+
262+
assertEquals(expected, transpose.transpose(input));
263+
}
264+
}

0 commit comments

Comments
 (0)