Sha256: 4fb371b84187f47439bd484f1016c3d92d657b5b89628f8aeb3069ea7a349fb9

Contents?: true

Size: 1.08 KB

Versions: 188

Compression:

Stored size: 1.08 KB

Contents

import Foundation

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(String(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

188 entries across 188 versions & 1 rubygems

Version Path
trackler-2.2.0.5 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.0.4 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.0.3 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.0.2 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.0.1 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.0.0 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.55 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.54 tracks/swift/exercises/transpose/Sources/TransposeExample.swift