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.1.180 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.179 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.178 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.177 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.176 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.175 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.174 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.173 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.172 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.171 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.170 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.169 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.167 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.166 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.165 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.164 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.163 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.162 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.161 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.160 tracks/swift/exercises/transpose/Sources/TransposeExample.swift