Created
August 17, 2019 22:04
-
-
Save mohitathwani/ed220e4bdd5d9fcddde528ebf1487730 to your computer and use it in GitHub Desktop.
Weaving two arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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