Skip to content

Commit 30fc6d6

Browse files
committed
Transpose exercise implemented
1 parent f9fa345 commit 30fc6d6

6 files changed

Lines changed: 328 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@
329329
"slug": "kindergarten-garden",
330330
"difficulty": 1,
331331
"topics": []
332+
},
333+
{
334+
"slug": "transpose",
335+
"difficulty": 1,
336+
"topics": []
332337
}
333338
],
334339
"deprecated": [

‎exercises/settings.gradle‎

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

0 commit comments

Comments
 (0)