Sha256: 124b30ce2ade6718f38ac9f00a0a2bf6190ab2f688c6b6406caf204c58aadc57

Contents?: true

Size: 1.08 KB

Versions: 137

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

137 entries across 137 versions & 1 rubygems

Version Path
trackler-2.1.0.53 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.52 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.51 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.50 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.49 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.48 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.47 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.46 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.45 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.44 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.43 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.42 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.41 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.40 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.39 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.38 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.37 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.36 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.34 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.1.0.33 tracks/swift/exercises/transpose/Sources/TransposeExample.swift