zwvista

导航

趣味编程:静夜思(Swift版)

func verticalWriting(txt:String, offset:Int) {
    Dictionary(grouping: txt.enumerated(), by: {$0.0 % offset})
        .sorted{$0.0 < $1.0}
        .forEach{print(
            $0.1.map{String($0.element)}.reversed().joined(separator: "|")
        )}
}

verticalWriting(txt: "床前明月光疑是地上霜举头望明月低头思故乡", offset: 5)

/*
低|举|疑|床
头|头|是|前
思|望|地|明
故|明|上|月
乡|月|霜|光
*/

posted on 2017-05-27 02:01  zwvista  阅读(184)  评论(0编辑  收藏  举报