exercism/go/transpose/transpose.go

33 lines
585 B
Go
Raw Normal View History

2017-08-12 14:01:07 +00:00
package transpose
import (
"strings"
)
const testVersion = 1
func Transpose(inp []string) []string {
// find the longest length string in the slice
lng := 0
for i := range inp {
if len(inp[i]) > lng {
lng = len(inp[i])
}
}
ret := make([]string, lng, lng)
for i := range inp {
for j := 0; j < lng; j++ {
if len(inp[i]) > j {
ret[j] = ret[j] + string(inp[i][j])
} else {
ret[j] = ret[j] + " "
}
}
}
if len(ret) > 0 {
// Trim the spaces off of the end of the last line
ret[len(ret)-1] = strings.TrimRight(ret[len(ret)-1], " ")
}
return ret
}