1
exercism fetch go transpose

cases_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package transpose

// Source: exercism/problem-specifications
// Commit: 6dba022 transpose: Fix canonical-data.json formatting
// Problem Specifications Version: 1.0.0

var testCases = []struct {
	description string
	input       []string
	expected    []string
}{
	{
		"empty string",
		[]string{},
		[]string{},
	},
	{
		"two characters in a row",
		[]string{
			"A1",
		},
		[]string{
			"A",
			"1",
		},
	},
	{
		"two characters in a column",
		[]string{
			"A",
			"1",
		},
		[]string{
			"A1",
		},
	},
	{
		"simple",
		[]string{
			"ABC",
			"123",
		},
		[]string{
			"A1",
			"B2",
			"C3",
		},
	},
	{
		"single line",
		[]string{
			"Single line.",
		},
		[]string{
			"S",
			"i",
			"n",
			"g",
			"l",
			"e",
			" ",
			"l",
			"i",
			"n",
			"e",
			".",
		},
	},
	{
		"first line longer than second line",
		[]string{
			"The fourth line.",
			"The fifth line.",
		},
		[]string{
			"TT",
			"hh",
			"ee",
			"  ",
			"ff",
			"oi",
			"uf",
			"rt",
			"th",
			"h ",
			" l",
			"li",
			"in",
			"ne",
			"e.",
			".",
		},
	},
	{
		"second line longer than first line",
		[]string{
			"The first line.",
			"The second line.",
		},
		[]string{
			"TT",
			"hh",
			"ee",
			"  ",
			"fs",
			"ie",
			"rc",
			"so",
			"tn",
			" d",
			"l ",
			"il",
			"ni",
			"en",
			".e",
			" .",
		},
	},
	{
		"square",
		[]string{
			"HEART",
			"EMBER",
			"ABUSE",
			"RESIN",
			"TREND",
		},
		[]string{
			"HEART",
			"EMBER",
			"ABUSE",
			"RESIN",
			"TREND",
		},
	},
	{
		"rectangle",
		[]string{
			"FRACTURE",
			"OUTLINED",
			"BLOOMING",
			"SEPTETTE",
		},
		[]string{
			"FOBS",
			"RULE",
			"ATOP",
			"CLOT",
			"TIME",
			"UNIT",
			"RENT",
			"EDGE",
		},
	},
	{
		"triangle",
		[]string{
			"T",
			"EE",
			"AAA",
			"SSSS",
			"EEEEE",
			"RRRRRR",
		},
		[]string{
			"TEASER",
			" EASER",
			"  ASER",
			"   SER",
			"    ER",
			"     R",
		},
	},
	{
		"many lines",
		[]string{
			"Chor. Two households, both alike in dignity,",
			"In fair Verona, where we lay our scene,",
			"From ancient grudge break to new mutiny,",
			"Where civil blood makes civil hands unclean.",
			"From forth the fatal loins of these two foes",
			"A pair of star-cross'd lovers take their life;",
			"Whose misadventur'd piteous overthrows",
			"Doth with their death bury their parents' strife.",
			"The fearful passage of their death-mark'd love,",
			"And the continuance of their parents' rage,",
			"Which, but their children's end, naught could remove,",
			"Is now the two hours' traffic of our stage;",
			"The which if you with patient ears attend,",
			"What here shall miss, our toil shall strive to mend.",
		},
		[]string{
			"CIFWFAWDTAWITW",
			"hnrhr hohnhshh",
			"o oeopotedi ea",
			"rfmrmash  cn t",
			".a e ie fthow ",
			" ia fr weh,whh",
			"Trnco miae  ie",
			"w ciroitr btcr",
			"oVivtfshfcuhhe",
			" eeih a uote  ",
			"hrnl sdtln  is",
			"oot ttvh tttfh",
			"un bhaeepihw a",
			"saglernianeoyl",
			"e,ro -trsui ol",
			"h uofcu sarhu ",
			"owddarrdan o m",
			"lhg to'egccuwi",
			"deemasdaeehris",
			"sr als t  ists",
			",ebk 'phool'h,",
			"  reldi ffd   ",
			"bweso tb  rtpo",
			"oea ileutterau",
			"t kcnoorhhnatr",
			"hl isvuyee'fi ",
			" atv es iisfet",
			"ayoior trr ino",
			"l  lfsoh  ecti",
			"ion   vedpn  l",
			"kuehtteieadoe ",
			"erwaharrar,fas",
			"   nekt te  rh",
			"ismdsehphnnosa",
			"ncuse ra-tau l",
			" et  tormsural",
			"dniuthwea'g t ",
			"iennwesnr hsts",
			"g,ycoi tkrttet",
			"n ,l r s'a anr",
			"i  ef  'dgcgdi",
			"t  aol   eoe,v",
			"y  nei sl,u; e",
			",  .sf to l   ",
			"     e rv d  t",
			"     ; ie    o",
			"       f, r   ",
			"       e  e  m",
			"       .  m  e",
			"          o  n",
			"          v  d",
			"          e  .",
			"          ,",
		},
	},
}

transpose_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package transpose

import (
	"reflect"
	"testing"
)

func TestTranspose(t *testing.T) {
	for _, test := range testCases {
		actual := Transpose(test.input)
		if !reflect.DeepEqual(actual, test.expected) {
			// check for zero length slices
			if len(actual) == 0 || len(test.expected) == 0 {
				t.Fatalf("\n\tTranspose(%q): %s\n\n\tExpected: %q\n\tGot: %q",
					test.input, test.description, test.expected, actual)
			}
			// let's make the error more specific and find the row it's on
			min := min(len(test.expected), len(actual))
			for i := 0; i < min; i++ {
				if test.expected[i] != actual[i] {
					t.Fatalf("\n\tTranspose(%q): %s\n\n\tExpected: %q\n\tGot: %q\n\n\tRow %d Expected: %q Got: %q",
						test.input, test.description, test.expected, actual, i, test.expected[i], actual[i])
				}
			}
		}
	}
}

// helper function
// https://stackoverflow.com/questions/27516387/what-is-the-correct-way-to-find-the-min-between-two-integers-in-go
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func BenchmarkTranspose(b *testing.B) {
	for _, test := range testCases {
		for i := 0; i < b.N; i++ {
			Transpose(test.input)
		}
	}
}