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.0.8.28 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.27 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.26 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.24 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.23 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.22 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.21 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.20 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.19 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.18 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.17 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.16 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.15 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.14 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.13 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.12 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.11 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.10 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.9 tracks/swift/exercises/transpose/Sources/TransposeExample.swift
trackler-2.0.8.8 tracks/swift/exercises/transpose/Sources/TransposeExample.swift