import Foundation
// using loops
func transpose<T>(_ matrix: [[T]]) -> [[T]] {
let rows = matrix.count
let cols = matrix[0].count
var result = Array(repeating: Array(repeating: matrix[0][0], count: rows), count: cols)
for i in 0..<rows {
for j in 0..<cols {
result[j][i] = matrix[i][j]
}
}
return result
}
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
let t = transpose(matrix)
t.forEach { print($0.map(String.init).joined(separator: " ")) }
/*
run:
1 4 7
2 5 8
3 6 9
*/