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.159 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.158 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.157 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.156 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.155 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.154 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.153 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.152 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.151 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.150 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.149 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.148 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.147 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.146 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.145 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.144 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.143 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.142 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.141 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.2.1.140 tracks/swift/exercises/transpose/Sources/TransposeExample.swift