import static org.codehaus.groovy.tools.Utilities.*
def headersCollecter(List strings){
if(strings.any{ it == [] })
[]
else
[strings.collect{it.head()}] + headersCollecter(strings.collect{it.tail()})
}
def sentences = ["あいうえお", "かきくけこ", "夏です", "あつい !"]
def maxLength = sentences.max( {a, b -> a.length() <=> b.length()} ).size()
def padString = sentences.any({it.getBytes().length > 1}) ? " " : " "
def newStrings = sentences.collect{ String sentence ->
sentence.padRight(maxLength, padString).toList()
}
def result = headersCollecter(newStrings).collect{it.reverse(false)}.collect{it.join()}
assert ["あ夏かあ", "つできい", "いすくう", " けえ", "! こお"] == result
assert """\
あ夏かあ
つできい
いすくう
けえ
! こお\
""" == result.join(eol()).normalize()
result.each{println it}