Sha256: a0d9ed1c560942ae1e53c156625150a3b75bb5e166d7b03be9e9cd922a650455
Contents?: true
Size: 1.06 KB
Versions: 42
Compression:
Stored size: 1.06 KB
Contents
struct Transpose { static func transpose(_ input: [String]) -> [String] { let maxLineLength = input.map { $0.characters.count }.max() guard let maxLength = maxLineLength else { return [] } var result = [String](repeatElement("", count: maxLength)) for i in 0..<maxLength { for line in input { if let start = line.index(line.startIndex, offsetBy: i, limitedBy: line.endIndex), let end = line.index(start, offsetBy: 1, limitedBy: line.endIndex) { let character = line[start..<end] result[i].append(character) } else { result[i].append(" ") } } } return result.map { stripTrailingWhitespace($0) } } private static func stripTrailingWhitespace(_ input: String) -> String { var result = input while result.hasSuffix(" ") { result.remove(at: result.index(before: result.endIndex)) } return result } }
Version data entries
42 entries across 42 versions & 1 rubygems