Skip to content

Instantly share code, notes, and snippets.

@mohitathwani
Created August 17, 2019 22:04
Show Gist options
  • Save mohitathwani/ed220e4bdd5d9fcddde528ebf1487730 to your computer and use it in GitHub Desktop.
Save mohitathwani/ed220e4bdd5d9fcddde528ebf1487730 to your computer and use it in GitHub Desktop.
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