Weaving two arrays
func weave(left: inout [Int], right: inout [Int], temp: inout [Int], results: inout [[Int]]) { | |
if left.isEmpty || right.isEmpty { | |
var result = temp | |
result.append(contentsOf: left) | |
result.append(contentsOf: right) | |
results.append(result) | |
return | |
} | |
let leftFirst = left.removeFirst() | |
temp.append(leftFirst) | |
weave(left: &left, right: &right, temp: &temp, results: &results) | |
temp.removeLast() | |
left.insert(leftFirst, at: 0) | |
let rightFirst = right.removeFirst() | |
temp.append(rightFirst) | |
weave(left: &left, right: &right, temp: &temp, results: &results) | |
temp.removeLast() | |
right.insert(rightFirst, at: 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment