-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transforms.go
43 lines (39 loc) · 938 Bytes
/
transforms.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
package kowalski
import "strings"
// Chunk takes the input, and splits it up into chunks of the given length. If the input is longer than the list of
// part lengths, the lengths will be repeated.
func Chunk(input string, parts ...int) []string {
var res []string
remaining := input
p := 0
for len(remaining) > 0 {
if len(remaining) >= parts[p] {
res = append(res, remaining[0:parts[p]])
remaining = remaining[parts[p]:]
} else {
res = append(res, remaining)
remaining = ""
}
p = (p + 1) % len(parts)
}
return res
}
// Transpose rotates the input text so rows become columns, and columns become rows.
func Transpose(input []string) []string {
var res []string
i := 0
found := true
for found {
found = false
line := strings.Builder{}
for j := range input {
if len(input[j]) > i {
line.WriteByte(input[j][i])
found = true
}
}
res = append(res, line.String())
i++
}
return res
}