Sha256: a0d9ed1c560942ae1e53c156625150a3b75bb5e166d7b03be9e9cd922a650455

Contents?: true

Size: 1.06 KB

Versions: 42

Compression:

Stored size: 1.06 KB

Contents

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

42 entries across 42 versions & 1 rubygems

Version Path
trackler-2.0.6.15 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.14 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.13 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.12 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.11 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.10 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.9 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.8 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.7 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.6 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.5 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.4 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.3 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.2 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.1 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.6.0 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.5.18 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.5.17 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.5.16 tracks/swift/exercises/transpose/TransposeExample.swift
trackler-2.0.5.15 tracks/swift/exercises/transpose/TransposeExample.swift